miércoles, 10 de junio de 2009

Disable SharePoint Event Firing from any source code

Sometimes we may be interested in disable events to perform some tests or processes but we're only able to execute DisableEventFiring() and EnableEventFiring() functions from a SPItemEventReceiver.

A possible solution is disable and enable event firing from any source code. To do that, you can follow the next steps:

  1. Add a new class in your namespace which inherits from SPItemEventReceiver.
  2. Create 2 functions in that new class to call EnableEventFiring() and DisableEventFiring().
Now, we can create an instance of that class in our main program class and will be able to use our functions to enable and disable events. The code should be like this:
namespace MyNamespace
{
public class MyEventFiring : SPItemEventReceiver
{
public MyEventFiring()
{
}

public void MyDisableEventFiring()
{
this.DisableEventFiring();
}

public void MyEnableEventFiring()
{
this.EnableEventFiring();
}
}

public class MyClass
{
//...
MyEventFiring mef = new MyEventFiring();
mef.MyDisableEventFiring();
item.Update(); //This Update can be executed withou event firings
mef.MyEnableEventFiring();
//...
}
}

No hay comentarios: