Syrinx on SharePoint

Syrinx SharePoint Team Blog
Need help on your project? info@syrinx.com, or toll free (888) 579-7469, press 1

News



Need help with your SharePoint project?

Syrinx works with clients throughout New England and across the United States to architect, design, develop, and deploy SharePoint implementations. Working on fully outsourced projects, as part of your team, helping to train your team, or rescuing projects in trouble, we are comfortable doing it all. Projects from a couple weeks to several months in duration, reference clients available. Contact us today - info@syrinx.com, or toll free (888) 579-7469 and press 1 to speak to someone now!

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

SPEventManagerProp

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:

SPEventReceiver

Code that uses the custom class to suppress event firing:

ItemUpdatingUsage

This code seems to be working fine, although I haven't put much testing into it yet.

I hope this helps,

-Ryan

Comments

SharePoint said:

I recently completed what I estimated to be a small project of taking an existing set of InfoPath forms

# January 5, 2009 8:29 PM

Disable item events firing during item update « Adrian Henke’s Blog said:

Pingback from  Disable item events firing during item update « Adrian Henke’s Blog

# January 29, 2010 11:09 AM