While developing event receivers we usually make use of this.EventFiringEnabled
with a value as false or true. I was looking for the purpose of this statement over web in any article or forum but didn't found any answers. I have seen code where this.EventFiringEnabled=false
is added before item.Update() method and this.EventFiringEnabled=true
is usually added in finally {} block. Could any one tell me the exact application of this statement and the which to write first and where?
-
sharepoint.stackexchange.com/questions/20261/…– TARUNJul 26, 2017 at 5:52
-
check this also andrewwburns.com/2011/01/05/…– TARUNJul 26, 2017 at 5:53
1 Answer
The purpose is to hinder an update on an item from triggering event receivers again.
It was added as a way to help developers handle the "infinite loop" of update events that would otherwise be triggered when you are doing an update on the item associated with the event receiver. Like below
- You update an item in the GUI, the event receiver is called
- Inside that event receiver you update the item, the event receiver is called
Inside that event receiver you update the item, the event receiver is called
...
...
and so on
By setting the EventFiringEnabled = false
just before doing the Update()
in your event receiver, there will not be a new call to the event receiver, so only step 1 and 2 above would be executed.
As mentioned in the posts added by @Tarun as comments, there are some caveats with using this method, for example when you are creating new threads inside of your event receiver.
Also, since the property is static
it will affect all code running in the same thread. Therefore it is important to reset the value. See a basic example below.
// Disable event firing
var originalEventFiringValue = EventFiringEnabled;
EventFiringEnabled = false;
// Do the update
item.Update();
// Reset event firing
EventFiringEnabled = originalEventFiringValue