Pages

Friday, December 11, 2009

How to stop event handler recursion

I was getting errors into event log when I was trying to update list in which I have written event handler code and again that code was executed from event receiver only, so it goes in loop, sometimes my page becomes very slow.

Solution:

I need to write code which will disabled event firing before when I was trying to update something into same list and then I need to enable event firing after my code.

SAMPLE CODE:

this.DisableEventFiring();

objtem.SystemUpdate(false);

this.EnableEventFiring();

Reference Links:

 

http://www.sharepoint-tips.com/2006/10/preventing-event-handler-recursion.html

Thank you!

5 comments:

  1. The only way to disable event firing is using DisableEventFiring() and EnableEventFirigin()

    Nothing else, SystemUpdate() is used for different things.

    ReplyDelete
  2. Thank you Mukul for your comment, yeah I know SystemUpdate() is used for different things, In my sample code I have used update method for specific requirement but main point here is DisableEventFiring() and EnableEventFirigin() method.

    ReplyDelete
  3. Well, and how to disable EventHandling outside Event Receiver? For example inside Workflow?

    ReplyDelete
  4. Hi Mate,

    This worked for me when I needed to disableeventfiring from a webpart.

    Try creating a class like the below

    class HandleEventFiringInWebPart : SPItemEventReceiver
    {

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

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

    }

    then in the custom workflow action\ custom workflow\ webpart or wherever your executing the code from, create an instane of the object like so ...

    HandleEventFiringInWebPart oEventRecieverHandler = new HandleEventFiringInWebPart();

    Then to disableeventfiring just exceute the method just before the code updates whatever list you need to like so ..

    //Need to disable event firing in the thread before attempting to update or add items

    oEventRecieverHandler.AccDisableEventFiring();

    ALWAYS REMEMBER to re-enable after you have finshed updating your list.

    I always do this by throwing it in the finally section like so ...

    finally
    {

    //Make usre you reanble events firing in thread, after it has finished doing it's stuff
    oEventRecieverHandler.AccEnableEventFiring();



    Kudos to whatever articles I ripped this off.

    ReplyDelete
  5. Thank you Brett for sharing information!

    ReplyDelete