DisableEventFiring() or EnableEventFiring() programmatically on a List or Document Library from anywhere


The credit for figuring this out goes to Paul and here is the original blog http://unclepaul84.blogspot.com/2007/12/disable-event-firing-in-sharepoint-when.html

I had used in workflows and application page without any issues in past 6 months…

If you need to disable event firing in sharepoint from anywhere except event handler and would like to .DisableEventFiring() or .EnableEventFiring() bcoz its available on the event properties, how you would do that ?

  • workflow
  • custom application / site page
  • any other application using sharepoint object model
  • Answer: Use this class as a helper class and call the DisableEventFiring() and EnableEventFiring() as below….

    Have fun cheers!!!

     
       1: public static class SPEventManagerWrapper
       2:     {
       3: 
       4:         private static readonly string _className = "Microsoft.SharePoint.SPEventManager";
       5: 
       6:         private static readonly string _eventFiringSwitchName = "EventFiringDisabled";
       7: 
       8:         private static Type _eventManagerType;
       9: 
      10:         /// gets the status of event firing on the current thread
      11: 
      12:         public static bool EventFiringDisabled
      13:         {
      14:             get { return GetEventFiringSwitchValue(); }
      15:         }
      16:         private static Type EventManagerType
      17:         {
      18:             get
      19:             {
      20:                 if (_eventManagerType == null)
      21: 
      22:                     GetEventManagerType();
      23:                 return _eventManagerType;
      24: 
      25:             }
      26:         }
      27: 
      28: 
      29:         /// enables event firing on the current thread
      30: 
      31:         public static void EnableEventFiring()
      32:         {
      33:             SetEventFiringSwitch(false);
      34:         }
      35: 
      36: 
      37: 
      38:         /// disables sharepoint event firing on the current thread
      39: 
      40:         public static void DisableEventFiring()
      41:         {
      42:             SetEventFiringSwitch(true);
      43:         }
      44: 
      45: 
      46: 
      47: 
      48: 
      49:         /// sets the event firing switch on Microsoft.SharePoint.SPEventManager class using reflection
      50: 
      51:         private static void SetEventFiringSwitch(bool value)
      52:         {
      53: 
      54:             PropertyInfo pi = EventManagerType.GetProperty(_eventFiringSwitchName, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
      55: 
      56:             pi.SetValue(null, value, null);
      57: 
      58:         }
      59: 
      60: 
      61: 
      62:         private static bool GetEventFiringSwitchValue()
      63:         {
      64: 
      65:             PropertyInfo pi = EventManagerType.GetProperty(_eventFiringSwitchName, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
      66: 
      67:             object val = pi.GetValue(null, null);
      68: 
      69:             return (bool)val;
      70:         }
      71: 
      72: 
      73:         private static Type GetEventManagerType()
      74:         {
      75: 
      76:             _eventManagerType = typeof(SPList).Assembly.GetType(_className, true);
      77: 
      78: 
      79: 
      80:             return _eventManagerType;
      81: 
      82:         }
      83: 
      84:     }
    Advertisements

    2 Comments on “DisableEventFiring() or EnableEventFiring() programmatically on a List or Document Library from anywhere”

    1. Anjali October 29, 2009 at 9:11 am #

      Is it possible to enable/disable single eventhandler?

      • atulchhoda January 13, 2010 at 12:08 am #

        Yes it will work for that particular list/document library to which event handler/worflow is attached to that particular list/document library

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: