7

How do I disable event firing in code that is not part of an ItemReceiver?

E.g.: I have an FeatureActivated event receiver which update a list (adds column) and updates all items to populate the new column. I want to do this without firing the 'ItemUpdated'-event for all items in the list.

Dribbel
  • 2,906
  • 3
  • 31
  • 45
  • What I don't understand is : Events firing may be disabled for which SharePoint objects/area/domain/scope : web ? Farm ? list ? Item ? Where is the association made ? Many thanks –  Apr 12 '13 at 15:07
  • @user16325 See this Question: http://sharepoint.stackexchange.com/questions/20261/what-is-the-scope-of-disableeventfiring – Dribbel Apr 13 '13 at 15:42

1 Answers1

20

You can use this approach - create class:

public class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
{
    private readonly bool oldValue;

    public DisabledItemEventsScope()
    {
        oldValue = EventFiringEnabled;
        EventFiringEnabled = false;
    }

    #region IDisposable Members

    public void Dispose()
    {
        EventFiringEnabled = oldValue;
    }

    #endregion
}

using:

using (var scope = new DisabledItemEventsScope())
{
   Item.Update();
}

from here

Sergei Sergeev
  • 11,618
  • 5
  • 32
  • 49
  • Nice and clean! – Dribbel Mar 15 '12 at 15:00
  • Should this work for updating any item properties without firing an event, or just in that specific situation? – thanby Jan 24 '13 at 14:16
  • This will work for any item update – Sergei Sergeev Jan 24 '13 at 14:48
  • What if I need to disable event firing on a list, i.e. when I don't have the context of the list in question. I have some SharePoint web part code that needs to update a property of an item in a list. And I want to disable event firing when updating the property of that item, since the list has a registered event receiver. – Web User Jan 06 '16 at 04:05