How to Disable Events from Firing on SharePoint 2013


Sathish Nadarajan
SharePoint MVP
Published On :   02 Nov 2014
Visit Count
Today :  10    Total :   17060
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


As long as we are in to SharePoint 2013 development, we would have come up with the Event Receivers. There are certain scenarios that we will be writing ItemUpdated Event. Again that is based on the business requirement. But there could be a situation like, on the ItemUpdated itself, we will be updating the same list item.

In detail, I have a list which has an ItemUpdated Event Receiver (Custom one, which developed by the developer). On the event receiver, we are trying to update some of the columns based on the entered column value. i.e., based on Column1, I am calculating the value for the Column2 and update the list item on the Event Receiver. In that case, what happens is, again and again, the Event Receiver triggers as ends up with a bottle neck.

To avoid this, we can disable the event firing before updating the List Item on the Event Receiver. After completing our updates, we need to toggle back the property.

The properly from the SPItemEventReceiver class is

 base.EventFiringEnabled = false;

As a developer, for sure, we will forgot to toggle back this properly. To avoid that, let us come up with a Class and use that class with a “Using” Statement.

The simple class would be like

 public sealed class DisableEventFiring : SPItemEventReceiver, IDisposable
     {
         bool _originalValue;
 
         public DisabledEventsScope()
         {
             // Save off the original value of EventFiringEnabled 
             _originalValue = base.EventFiringEnabled;
 
             // Set EventFiringEnabled to false to disable it 
             base.EventFiringEnabled = false;
         }
 
         public void Dispose()
         {
             // Set EventFiringEnabled back to its original value 
             base.EventFiringEnabled = _originalValue;
         }
     }
 

The above class, I made it as sealed to avoid exceptions on the MSOCAF report generation. (MSOCAF – Microsoft SharePoint Online Code Analysis Framework). This ensure that, this class cannot be inherited.

Now, let us see, how to use this.

 public override void ItemUpdated(SPItemEventProperties properties)
         {
             base.ItemUpdated(properties);
 using (DisabledEventsScope scope = new DisabledEventsScope())
                                 {
 //Update the Columns on the ListItem here.
 }
 }
 

As long as the Scope is active, no other events will get triggered.

Note: This will cause the Event Firing disabled on the Site Collection Level. Hence, None of the events would be triggered on this duration. As there is no other option, to disable specifically, we need to live with this. But anyhow, our ItemUpdated event may not be executed more than few milli seconds. With this assumption, we are proceeding.

Happy Coding.

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Scan