Event receiver circular events

In this post I am going to explain one basic problem in event receiver events when you call update or add method in the same event or from another event recursively.

For example :

I have a event receiver on a calendar list :

public override void ItemUpdated(SPItemEventProperties properties)

In the above event I have a method which does some updates for another list and finally get details from the updated list and updated data in same calendar list.

In the above scenario , the ItemUpdated will fire again.

to avoid this add this code in your ItemUpdated event. (this.EventFiringEnabled = false;this.EventFiringEnabled = true;)

public override void ItemUpdated(SPItemEventProperties properties)

{

try

{

this.EventFiringEnabled = false;

SPSite currentsite = properties.Site;

int itemid = properties.ListItemId;

SPList listname = properties.List;

SPListItem listitem = properties.ListItem;

GenerateInitialAgendaDocument.UpdateAgendaDetails(currentsite, itemid);

 

}

catch (Exception ex)

{

throw ex;

}

finally

{

this.EventFiringEnabled = true;

}

}

Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *