4

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?

2

1 Answer 1

6

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

  1. You update an item in the GUI, the event receiver is called
  2. Inside that event receiver you update the item, the event receiver is called
  3. 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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.