Tuesday, April 15, 2014

Disable Sharepoint item events firing during item update

In Sharepoint, the attached item events are fired whenever you perform an item.Update(),so if you to want to restrict it for event firing you can use following code,

class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
{
    bool oldValue;
    public DisabledItemEventsScope()
    {
        this.oldValue = base.EventFiringEnabled;
        base.EventFiringEnabled = false;
    }
    #region IDisposable Members
    public void Dispose()
    {
        base.EventFiringEnabled = oldValue;
    }
    #endregion
}

Now we can use this class together with the using syntax to create a scope within all item event firing is disabled.

using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
{
    item.SystemUpdate(); // will NOT fire events
}
item.SystemUpdate(); // will fire events again

or other way to achieve this are,



public static class SPListItemExtension
{
public static void UpdateDisableItemEvents(this SPListItem item)
{
using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
{
item.Update();
}
}
}

and call it in your code as follows:
item.UpdateDisableItemEvents();

0 comments:

Post a Comment