Wednesday, March 13, 2013

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. }  


Enable to execute -ed events synchronously or asynchronously in SharePoint 2010

In SharePoint 2007, -ed events are executed only asynchronously.
This causes some problems between the main thread the and sub thread. For instance, -ed event thread sometimes cannot get its own item's "afterproperties".


Using SharePoint 2010, -ed events can be executed either synchronously or asynchronously.

The two Event Receivers are as follows:

  • -ing events (ItemAdding, ListAdding, etc)
    • synchronously 
  • -ed events (ItemAdded, ListAdded, etc)
    • synchronously or asynchronously

You can write "Synchronous" in Elements.xml in SharePoint EventReciver Project in Visual Studio 2010.

  1. <Synchronization>Synchronous</Synchronization>  

Listed below are the steps for making a synchronous -ed event:

  • Create project as Event Receiver.



  • Choose 2 events (ItemAdding and ItemAdded).



  • Write the code. I added the current thread name in ULS log in ItemAdding and ItemAdded method to see status of thread. It is also necessary to add Microsoft.SharePoint.Administration namespace.
  1. using System;  
  2. using System.Security.Permissions;  
  3. using Microsoft.SharePoint;  
  4. using Microsoft.SharePoint.Security;  
  5. using Microsoft.SharePoint.Utilities;  
  6. using Microsoft.SharePoint.Workflow;  
  7. using Microsoft.SharePoint.Administration;  
  8.   
  9. namespace EventReceiverProject4.EventReceiver1  
  10. {  
  11.     /// <summary>  
  12.     /// List Item Events  
  13.     /// </summary>  
  14.     public class EventReceiver1 : SPItemEventReceiver  
  15.     {  
  16.        /// <summary>  
  17.        /// An item is being added.  
  18.        /// </summary>  
  19.        public override void ItemAdding(SPItemEventProperties properties)  
  20.        {  
  21.            System.Threading.Thread.CurrentThread.Name = "Current Thread";  
  22.            string theadName = System.Threading.Thread.CurrentThread.Name;  
  23.   
  24.            SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("ItemAdding", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, theadName, theadName);  
  25.            base.ItemAdding(properties);  
  26.        }  
  27.   
  28.        /// <summary>  
  29.        /// An item was added.  
  30.        /// </summary>  
  31.        public override void ItemAdded(SPItemEventProperties properties)  
  32.        {  
  33.            string theadName = System.Threading.Thread.CurrentThread.Name;  
  34.            SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("ItemAdded", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, theadName, theadName);  
  35.            base.ItemAdded(properties);  
  36.        }  
  37.     }  
  38. }  

The ItemAdding and ItemAdded thread names are different because ItemAdded is run asynchronously.
The results are shown here:
  • ItemAdding's thread name : Current Thread
  • ItemAdded's thread name : null
Next, I added <Synchronization>Synchronous</Synchronization> in Elements.xml.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  3.   <Receivers ListTemplateId="104">  
  4.       <Receiver>  
  5.         <Name>EventReceiver1ItemAdding</Name>  
  6.         <Type>ItemAdding</Type>  
  7.         <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>  
  8.         <Class>EventReceiverProject4.EventReceiver1.EventReceiver1</Class>  
  9.         <SequenceNumber>10000</SequenceNumber>  
  10.         <Synchronization>Synchronous</Synchronization>  
  11.       </Receiver>  
  12.       <Receiver>  
  13.         <Name>EventReceiver1ItemAdded</Name>  
  14.         <Type>ItemAdded</Type>  
  15.         <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>  
  16.         <Class>EventReceiverProject4.EventReceiver1.EventReceiver1</Class>  
  17.         <SequenceNumber>10000</SequenceNumber>  
  18.       </Receiver>  
  19.   </Receivers>  
  20. </Elements>  
The Results are shown here: 
  • ItemAdding's thread name : Current Thread
  • ItemAdded's thread name : Current Thread

No comments:

Post a Comment