Monday, December 3, 2007

Disable event firing in SharePoint when updating a list item outside of an event handler

As you probably know, when developing Event Handlers for SharePoint you have the option turning off event firing. This functionality is exposed through SPEventReceiverBase.DisableEventFiring() Method . This is very useful when you are updating the item from within the event handler and do not want an infinite loop to occur.


During my last project i discovered that you cannot disable event firing from code that is not part of an event handler, because DisableEventFiring() is an internal instance method. Still, i was determined to find a solution. So i whipped out Reflector and started investigating. Upon disassembling Microsoft.Sharepoint.dll, I discovered that the above mentioned method actually sets a static, thread-specific, property of SPEventManager class called EventFiringDisabled. SPEventManager class is responsible for instantiating event handlers and delivering events to them. Because this class is marked "internal", it is not intended for use by SharePoint API consumers . But what is a good solution without some HACKS? :) I used reflection to set EventFiringDisabled property.


Below, you will find my solution neatly wrapped in a class. Please use at your own risk.



using System;

using
System.Collections.Generic
;

using
System.Text
;

using
System.Reflection
;

using
Microsoft.SharePoint
;


/// provides access to the internal Microsoft.SharePoint.SPEventManager class by using reflection

/// sample usage:

/// SPEventManagerWrapper.DisableEventFiring();

/// SPList myList = SPContext.Current.Web.Lists["Shared Documents"];
/// myList.Items[0].Update();

/// SPEventManagerWrapper.EnableEventFiring();

public static class SPEventManagerWrapper

{

private static readonly string _className = "Microsoft.SharePoint.SPEventManager"
;

private static readonly string
_eventFiringSwitchName = "EventFiringDisabled"
;

private static
Type _eventManagerType
;




/// gets the status of event firing on the current thread

public static bool
EventFiringDisabled

{

get { return GetEventFiringSwitchValue();
}

}


private static
Type EventManagerType

{

get

{



if (_eventManagerType == null
)

GetEventManagerType()
;



return
_eventManagerType
;



}

}



/// enables event firing on the current thread

public static void
EnableEventFiring()

{

SetEventFiringSwitch(
false)
;



}



/// disables sharepoint event firing on the current thread

public static void
DisableEventFiring()

{

SetEventFiringSwitch(
true)
;



}





/// sets the event firing switch on Microsoft.SharePoint.SPEventManager class using reflection

private static void SetEventFiringSwitch(bool value
)

{

PropertyInfo pi
= EventManagerType.GetProperty(_eventFiringSwitchName, System.Reflection.BindingFlags.Static System.Reflection.BindingFlags.NonPublic)
;



pi.SetValue(null, value, null)
;



}



private static bool
GetEventFiringSwitchValue()

{

PropertyInfo pi
= EventManagerType.GetProperty(_eventFiringSwitchName, System.Reflection.BindingFlags.Static System.Reflection.BindingFlags.NonPublic)
;



object
val = pi.GetValue(null, null)
;



return
(bool)val
;



}



private static
Type GetEventManagerType()

{

_eventManagerType
= typeof(SPList).Assembly.GetType(_className, true)
;



return
_eventManagerType
;

}

}