Disable Sharepoint item events firing during item update

In Sharepoint, the attached item events are fired whenever you perform an item.Update()
Ryan posted a solution to this issue, using a custom event handler that disables the events upon request.

What I dislike about the solution, is the risk of leaving the events disabled. Assume an exception occurs, the code will never reach the line where the event firing is enabled (restored) again.

I suggest a using-pattern, much like the TransactionScope class used for db transactions in the entity framework.

    class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
    {
        public DisabledItemEventsScope()
        {
            base.DisableEventFiring();
        }

        #region IDisposable Members

        public void Dispose()
        {
            base.EnableEventFiring();
        }

        #endregion
    }

Now we can use this class together with the using syntax to create a scope within all item event firing is disabled.

                using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
                {
                    item.SystemUpdate(); // will NOT fire events
                }
                item.SystemUpdate(); // will fire events again

This way, the event firing is enabled again as soon as the code leaves the defined scope, be it through normal operation or by exceptions.

Update 1

For SharePoint 2010, use this snippet instead:

    /// <summary>
    /// Disabled item events scope
    /// </summary>
    /// <see cref="http://adrianhenke.wordpress.com/2010/01/29/disable-item-events-firing-during-item-update/"/>
    class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
    {
        bool oldValue;

        public DisabledItemEventsScope()
        {
            this.oldValue = base.EventFiringEnabled;
            base.EventFiringEnabled = false;
        }

        #region IDisposable Members

        public void Dispose()
        {
            base.EventFiringEnabled = oldValue;
        }

        #endregion
    }
About these ads

24 Responses to “Disable Sharepoint item events firing during item update”


  1. 1 Azrael Palatot March 2, 2010 at 6:45 am

    Thanks!!! it works

  2. 2 Simone Ferrazzo August 4, 2010 at 10:04 am

    Great, this solution is simple and very bright, but instead of use the using-pattern is not better use a try-catch-finally? Because if code generate an exception inside the using statement, the event handler is not more enabled!

  3. 3 adrianhenke August 5, 2010 at 1:55 pm

    The “using” statement is being translated to try/catch/finally anyway. Therefore if an exception occurs within a using block, it will call the Dispose(..) method of the using-object :)

  4. 4 Kanchan November 15, 2010 at 4:13 pm

    i am a beginner at SP workflow and facing the same issue. I have a few fields that are updated as part of workflow, i dont want to trigger a new instance when those are updated. I understand the logic but can you show me where to update the event handler?

  5. 6 Adam Carr January 28, 2011 at 1:49 pm

    base.DisableEventFiring() & base.EnableEventFiring() have both been made obsolete in SharePoint 2010.

    Use the boolean property base.EventFiringEnabled instead.

  6. 7 Tracy March 16, 2011 at 3:49 pm

    Such a clean solution! Thanks <3

  7. 8 sdf June 9, 2011 at 5:44 pm

    much gr8 post !!!

  8. 10 Matthew Chestnut July 5, 2012 at 5:56 pm

    This solution was a HUGE help in our battle to resolve “The sandboxed code execution request was refused because the Sandboxed Code Host Service was too busy to handle the request.”

    This particular error only occurred in some environments, not all. We opened a Microsoft support case, too, but ended up coming up with a workaround ourselves based on this method of disabling event handlers.

    Our traditional method of disabling event handlers from firing was to set “this.EventFiringEnabled = false” in, for example, the ItemUpdating event handler.

    However, we discovered in some environments, for some unknown reason, the flag would be reset to true and the event handlers would fire recursively, thereby causing the [not very helpful] “too busy to handle the request” error.

  9. 11 Robin January 23, 2013 at 9:30 am

    can we use this code in timer job?

  10. 12 Alvar Kresh August 2, 2013 at 1:04 pm

    Simple and efficient…. Thanks !

  11. 13 Joshua Orimogunje September 10, 2013 at 12:10 pm

    Add to my a little spicy to the code, you can turn it to an extension class as follows:

    public static class SPListItemExtension
    {
    public static void UpdateDisableItemEvents(this SPListItem item)
    {
    using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
    {
    item.Update();
    }
    }
    }

    and call it in your code as follows:
    item.UpdateDisableItemEvents();

  12. 15 AR November 8, 2013 at 4:05 am

    Doesn’t seem to work in SharePoint 2013.

  13. 16 Alyssanaf February 5, 2014 at 4:53 am

    фильм онлайн робокоп 2014 робокоп 2014 торрент робокоп robocop 2014 смотреть онлайн новый робокоп 2014 новый робокоп 2014 трейлер робокоп 2014 скачать торрент http://robocop-smotret-online.umi.ru/#7331 робокоп 2014 трейлер к фильму робокоп 2014 робокоп robocop 2014 робокоп 2014 трейлер русский язык смотреть кино 2014 робокоп

  14. 17 Mitzi May 8, 2014 at 10:26 pm

    Thanks in favor of sharing such a pleasant opinion, paragraph is
    good, thats why i have read it fully

  15. 18 Luca June 14, 2014 at 6:53 am

    It’s appropriate time to mak sme plpans for thhe futhre
    andd it iis time tto be happy. I haave read tthis posxt and iif I ciuld I wnt to suggest yoou some interresting things oor suggestions.
    Maybe yoou could wrijte next articles referring to this article.
    I wish too rewd ven mopre thibgs about it!

  16. 19 Lewis June 24, 2014 at 11:14 pm

    I’m not that much of a online reader to be honest but your sites really nice, keep
    it up! I’ll go ahead and bookmark your site to come back down the road.
    Cheers

  17. 20 CLEP Human Growth and Development 8th Ed.: 3 (CLEP Test Preparation) pdf August 6, 2014 at 2:54 pm

    Thanks for your marvelous posting! I definitely enjoyed reading it, you may be
    a great author.I will make sure to bookmark your blog and will often come back down the road.
    I want to encourage you to definitely continue your great work,
    have a nice afternoon!

  18. 21 Tandem Fishing Kayak September 20, 2014 at 7:53 pm

    Hi there, the whole thing is going fine here and ofcourse every one is
    sharing facts, that’s really good, keep up writing.


  1. 1 Disable Event Firing Using a Using Statement and Inheritance « Sladescross's Blog Trackback on May 8, 2011 at 4:13 pm
  2. 2 Stop SharePoint events firing | 3 Guys On SharePoint Blog Trackback on September 14, 2011 at 3:00 pm
  3. 3 Curia Damiano blog | Disabling event firing outside an event receiver Trackback on November 22, 2013 at 4:13 pm

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





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: