A blog about SharePoint, Project Server and other Microsoft technologies.

Thursday, October 25, 2012

Disable event firing - Part 1 (in event receiver)

If you are using Event Receivers for Add, Update and Delete operations on a Sharepoint list item, you might experience some issues concerning multiple event firing.Firstly, what are Event Receivers?

An event receiver in MS SharePoint a method that is called when a triggering action occurs on a
specified SharePoint object. This triggering actions may be various actions such as
updating, adding, moving, deleting, checking in, and checking out. That means that if you register a event receiver ItemAdding to your list, every time you add new item to your list, that event will trigger, and custom code that you put in your Event Receiver will be executed. Same happens with ItemUpdating event if you update list item.Problem appears when you add code to event receiver that updates (or inserts new item) SPListItem. That is going to trigger ItemUpdating (or ItemAdding) event again, and again, and again...To prevent that use this code in your event receiver:

publicclassHandleEventFiring : SPItemEventReceiver

{

publicvoid
DisableEventFiring()

{

this.EventFiringEnabled = false;

}

publicvoid
EnableEventFiring()

{

this.EventFiringEnabled = true;

}

}

In the next post I'll show you how to disable/enable event firing outside of an event receiver.

2 comments:

Good quick references for this topic. It should be noted that the updated/updating event receiver loop is not infinite as Microsoft put hooks in so that it only executes 10 total times if you accidentally have this happen.