Disabling event firing on SPListItem.Update() differently
For those of you out there who have (or have not) written custom Event Receivers to manage the adding, updating, deleting, etc. of your list items at the coding level may have run into this problem. My particular problem was that I was writing a workflow application that needed to update the underlying List Item tied to the workflow at various points along the State Machine process. I'm sure this is extremely common, as permissions need to be managed, metadata needs to be updated, etc. as an item moves through business process. When this happens I don't want a version created or any of my custom event receiver code to fire.
What I found was disappointing. Using SystemUpdate() helps to prevent version incrementing, but will not disable event firing. You can call DisableEventFiring() in the context of a SPItemEventReceiver, but no where else in SharePoint seems to support this functionality. So, I used .NET Reflector to open the SharePoint assembly to track down what this method actually does.
If you open the assembly and look at the SPItemEventReceiver class, you see it has a base class called SPEventReceiverBase. In this class are only 2 methods: EnableEventFiring() and DisableEventFiring(). Both set SPEventManager.EventFiringDisabled (bool). If you look at this Class and property (all by using disassemble) in .NET Reflector, you can see what the actual property is doing in the image below
Before seeing what this code was doing under the covers, I was worried about creating my own Class that inherited from SPItemEventReceiver and calling the DisableEventFiring() and EnableEventFiring() methods manually. I had no idea if this was specific to the user context, or disabled events for that period of time across the entire application. Thanks to Reflector I feel comfortable calling this code because it is based on the thread and should not affect other users of the application and their subsequent list item updates.
The two images below show what I did to solve this problem. I created a lightweight class that contains methods for disabling and enabling event firing on the thread. All my code then uses this class when performing system level updates where I don't want to raise any of the List Item events.
Custom Class:
Code that uses the custom class to suppress event firing:
This code seems to be working fine, although I haven't put much testing into it yet.
I hope this helps,
-Ryan