How to disabling event firing in SharePoint 2010

 
Today, I tested whether event receiver fires or not when I use SPListItem's Update methods.
The results are here.


Methods                 event firing
Update()                   fired
SystemUpdate()             fired
SystemUpdate(false)        fired
UpdateOverwriteVersion()   fired

All methods fired !!

But I think people sometimes want not to fire a event receiver when a update method is used.
So, I would like to introduce how to disable event firing.


In SharePoint 2010, SPEventReceiverBase.DisableEventFiring Method and SPEventReceiverBase.EnableEventFiring Method are already obsolete. We need to use EventFiringEnabled Property.



A sample code is here.

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     const string siteUrl = "http://sp2010";  
  4.     SPSite site = new SPSite(siteUrl);  
  5.     SPWeb web = site.RootWeb;  
  6.   
  7.     SPList list = web.Lists["Announcements"];  
  8.     SPListItem item = list.Items[3];  
  9.   
  10.     HandleEventFiring handleEventFiring = new HandleEventFiring();  
  11.     handleEventFiring.DisableHandleEventFiring();  
  12.   
  13.     try  
  14.     {  
  15.         item.Update();  
  16.     }  
  17.     finally  
  18.     {  
  19.         handleEventFiring.EnableHandleEventFiring();  
  20.     }  
  21. }  
  22.   
  23. public class HandleEventFiring : SPItemEventReceiver  
  24. {  
  25.   
  26.     public void DisableHandleEventFiring()  
  27.     {  
  28.         //obsolete  
  29.         //this.DisableEventFiring();  
  30.         this.EventFiringEnabled = false;  
  31.     }  
  32.   
  33.     public void EnableHandleEventFiring()  
  34.     {  
  35.         //obsotete  
  36.         //this.EnableEventFiring();  
  37.         this.EventFiringEnabled = true;  
  38.     }  
  39. }  

http://stackoverflow.com/questions/5187119/sharepoint-workflow-is-still-recursive-on-item-change-even-with-sp2  Sharepoint Workflow is still recursive on item change even with sp2

http://www.mattjimison.com/blog/2011/05/11/disable-event-receivers-during-an-update/  Disable Event Receivers During an Update

http://flylib.com/books/en/4.221.1.37/1/  Inside Microsoft Windows Sharepoint Services Version 3  Event Receivers

http://sharepointgauravgoyal.blogspot.com/2012/08/make-read-only-or-hide-fields-in.html

http://weblogs.asp.net/bsimser/archive/2010/04/12/using-jquery-and-spservices-to-display-list-items.aspx

posted on 2012-10-29 22:31  ilawrence  阅读(283)  评论(0编辑  收藏  举报