When we are in List Item Event Receiver code, we can modify/update the same item which will fire the event receiver again. For disabling event receiver to get fired again we can use the property ‘’ as shown below:
public class TestEventReceiver:SPItemEventReceiverFigure 1: Sample code to enable/disable event receiver firing inside event receiver handler.
{
public override void ItemAdded(SPItemEventProperties properties)
{
//disable event receiver firing
EventFiringEnabled = false;
//do something
//enalbe event receiver firing
EventFiringEnabled = true;
}
}
However, if you are in a webpart and want to modify an item but don’t want to fire event receiver, then? Don’t worry there’s a way out. I’ll explain this today.
What happens when Event Receiver disabled?
When you disable event receiver, SharePoint internally set a data field in current Thread. So if you can set your current’s thread’s data to the required value before/after updating item, you can control the event receiver. However, setting current thread data manually might be risky and I’ll not use that path. Rather I’ll show how we can use existing power of ‘SPEventReceiverBase’ to control event receiver firing.
Create your own Event Receiver Controller
I’ve create a custom class inherited from ‘SPEventReceiverBase’. From this base class I get a properties ‘EventFiringEnabled’ which allows me to control the event receiver firing. The following code snippet shows my custom EventReceiverManager:
public class EventReceiverManager : SPEventReceiverBase, IDisposableFigure 2: A EventReceiverManager custom class to control event receiver firing.
{
public EventReceiverManager(bool disableImmediately)
{
EventFiringEnabled = !disableImmediately;
}
public void StopEventReceiver()
{
EventFiringEnabled = false;
}
public void StartEventReceiver()
{
EventFiringEnabled = true;
}
public void Dispose()
{
EventFiringEnabled = true;
}
}
The code snippet above is derived from SharePoint’s SPEventReceiverBase to use the ‘EventFireingEnabled’ property.
Now you can use this EventReceiverManager to control the event receiver. To stop firing the event receiver on any changes, you need to wrap the code block inside EventReceiverManager as shown below:
using (var eventReceiverManager = new EventReceiverManager(true))Figure 3: How to use EventReceiverManager to disable event receiver firing.
{
var list = GetList("listName");
var listItem = list.GetItemById(itemId);
listItem["field"] = "value";
listItem.Update();
}
As shown above even if he list has event receiver for ItemUpdated/ItemDating the event receiver will not get fired because of putting the code in EventReceiverManager block. Please notice of ‘using’ block, as soon as you leave the ‘using’ block, the event firing in enabled automatically. This is because in Dispose method of EventReceiverManager I’ve enabled the event firing.
Thank you, this seems to be just the code I needed.
ReplyDeleteThanks. This is very useful tip.
ReplyDeleteVery nifty little tip! Just for my interest's sake, can you explain how your EventReceiverManager class actually "hooks in" to the event receivers that are triggered inside the using block? And if I were to update multiple lists inside the using block, would event firing be disabled on ALL of the event receivers?
ReplyDelete@Nick, it's all about threading. Setting the EventFiringEnabled property, set a custom value in the current thread. So as long as you do all your work with the current thread, you should get the event firing active/deactived.
ReplyDeleteBad example of code. Dont use this if you professional.
ReplyDeleteTerrible example of a comment. If you are going to assert that there is something wrong, explain your reasons
Delete