Avoid recursive updates in event handler


I wrote a ItemUpdated event for a custom List (lets assume the list name is mylist) and my custom ItemUpdated event again updating a field value of the same mylist.

In this scenario the ItemUpdate event is getting triggered again and again (i don't remember how many times:)), as we call the update method for the same list inside the event handler(i have given below the code)


Code that updates list recursively in ItemUpdated Event


public override void ItemUpdated(SPItemEventProperties properties)
{
using (spWeb = properties.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;
properties.ListItem["MyColumnName"] = "NewValueToUpdate";
properties.ListItem.SystemUpdate(false);
spWeb.AllowUnsafeUpdates = false;
}
}



To Avoid this recursive call, eventhanler has DisableEventFiring() method before the Update method is getting called and pls dont forget to call EnableEventFiring() method after the Update.


Corrected Code which will not call ItemUpdated event recurcively


public override void ItemUpdated(SPItemEventProperties properties)
{
using (spWeb = properties.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;
//
this.DisableEventFiring();
//
properties.ListItem["MyColumnName"] = "NewValueToUpdate";
properties.ListItem.SystemUpdate(false);
//
this.EnableEventFiring();
spWeb.AllowUnsafeUpdates = false;
}
}