Disable event firing in SharePoint Event Receiver

If you want to update the current item metadata or other item metadata in other list or library and also does NOT need to fire another event for the other item you’ve updated.

In this article, I would like to show you the best way to do it.

///
/// Disabled item events scope
/// 
class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
{
   bool oldValue;
   public DisabledItemEventsScope()
   {
      this.oldValue = base.EventFiringEnabled;
      base.EventFiringEnabled = false;
   }
   public void Dispose()
   {
      base.EventFiringEnabled = oldValue;
   }
}

and in your event receiver method ItemAdded, ItemUpdated, or others

public override void ItemUpdated(SPItemEventProperties properties)
{
   base.ItemUpdated(properties);
   SPListItem currItem = properties.ListItem;
   using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
   {
      currItem["Column2"] = "updated";
      currItem.Update();
   }
}
Advertisements

Tagged: , , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: