Disable Event Receiver From non-receiver code in SharePoint

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:SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
//disable event receiver firing
EventFiringEnabled = false;
//do something
//enalbe event receiver firing
EventFiringEnabled = true;
}
}







Figure 1: Sample code to enable/disable event receiver firing inside event receiver handler.



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, IDisposable
{
public EventReceiverManager(bool disableImmediately)
{
EventFiringEnabled = !disableImmediately;
}
public void StopEventReceiver()
{
EventFiringEnabled = false;
}
public void StartEventReceiver()
{
EventFiringEnabled = true;
}
public void Dispose()
{
EventFiringEnabled = true;
}
}







Figure 2: A EventReceiverManager custom class to control event receiver firing.



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))
{
var list = GetList("listName");
var listItem = list.GetItemById(itemId);
listItem["field"] = "value";
listItem.Update();
}







Figure 3: How to use EventReceiverManager to disable event receiver firing.



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.