Real Disable Event Firing :)

Posted: 22nd October 2013 by Gennady in C#, SharePoint
Tags: ,

Sometimes in SharePoint event handlers You need to freeze events and update element “silently”. There are several examples in the internet how to do it, but they does not always work or work only in special context.

Here is a method that works even in console applications too.

It is an extension-class for SPListItem object. The idea was taken from post on stackoverflow, I added few methods to Add file and “CopyTo” function, that allows to copy file silently.

using Microsoft.SharePoint;
 
public static class SPListItemExtensions
{
 /// <summary>
 /// Provides ability to update list item without firing event receiver.
 /// </summary>
 /// <param name="item">list item</param>
 /// doNotFireEvents">Disables firing event receiver while updating item.
 public static void Update(this SPListItem item, bool doNotFireEvents)
 {
 SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
 if (doNotFireEvents)
 {
 try
 {
 rh.DisableEventFiring();
 item.Update();
 }
 finally
 {
 rh.EnableEventFiring();
 }
 }
 else
 {
 item.Update();
 }
 }
 
 /// <summary>
 /// Provides ability to update list item without firing event receiver.
 /// </summary>
 public static void SystemUpdate(this SPListItem item, bool incrementListItemVersion, bool doNotFireEvents)
 {
 SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
 if (doNotFireEvents)
 {
 try
 {
 rh.DisableEventFiring();
 item.SystemUpdate(incrementListItemVersion);
 }
 finally
 {
 rh.EnableEventFiring();
 }
 }
 else
 {
 item.SystemUpdate(incrementListItemVersion);
 }
 }
 
 /// <summary>
 /// Provides ability to copy file from list item without firing event receiver.
 /// </summary>
 public static void CopyTo(this SPFile file, string strNewUrl, bool bOverWrite, bool doNotFireEvents)
 {
 SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
 if (doNotFireEvents)
 {
 try
 {
 rh.DisableEventFiring();
 file.CopyTo(strNewUrl, bOverWrite);
 }
 finally
 {
 rh.EnableEventFiring();
 }
 }
 else
 {
 file.CopyTo(strNewUrl, bOverWrite);
 }
 }
 
 
 /// <summary>
 /// Provides ability to update list item without firing event receiver.
 /// </summary>
 public static void SystemUpdate(this SPListItem item, bool doNotFireEvents)
 {
 SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
 if (doNotFireEvents)
 {
 try
 {
 rh.DisableEventFiring();
 item.SystemUpdate();
 }
 finally
 {
 rh.EnableEventFiring();
 }
 }
 else
 {
 item.SystemUpdate();
 }
 }
 
 /// <summary>
 /// Provides ability to add file to sharepoint library without firing event receiver.
 /// </summary>
 public static SPFile AddFile(SPFileCollection spFileCollection, string destPathToFile, byte[] binFileData, bool overwrite, bool doNotFireEvents)
 {
 SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
 if (doNotFireEvents)
 {
 try
 {
 rh.DisableEventFiring();
 SPFile addedFile = spFileCollection.Add(destPathToFile, binFileData, overwrite);
 return addedFile;
 }
 finally
 {
 rh.EnableEventFiring();
 }
 }
 else
 {
 SPFile addedFile = spFileCollection.Add(destPathToFile, binFileData, overwrite);
 return addedFile;
 }
 }
 
 private class SPItemEventReceiverHandling : SPItemEventReceiver
 {
 public SPItemEventReceiverHandling() { }
 
 new public void DisableEventFiring()
 {
#pragma warning disable 612,618
 base.DisableEventFiring();
#pragma warning restore 612,618
 }
 
 new public void EnableEventFiring()
 {
#pragma warning disable 612,618
 base.EnableEventFiring();
#pragma warning restore 612,618
 }
 }
}

Click below to expand comments form..