A tricky work-around to avoid infinite loop while updating an item in ItemUpdated Event in a very special scenario.

I think this post’s title is little confusing, anyway here is another scenario to play with a List Event Handler. Recently I had worked with one my customers , he had implemented the code to send documents to a record center using SPFile.SendToOfficialFile() method. In the destination document library he had registered an event handler to catch the ItemUpdated event and update some metadata. Also there were several content types in the destination document library and there was an expiration policy for each content type which will automatically update a date time column “Expiration date”.

The issue was whenever he register the event handler it was not updating that column through the content type policy and we figured out that once we remove the this.DisableEventFiring & this.EnableEventFiring everything worked as expected, but then the major problem was from the ItemUpdated event he was updating the list item which results an infinite loop and wanted a work-around for that issue.

To resolve that infinite issue, we have created a static Boolean variable and updated its value whenever there is an ItemUpdated event and whenever there is a second ItemUpdated event fire due to the loop then we set the value to false and exits from the event. Thus this way it will not affect any other users and each time the static variable will set to false. Also to avoid any issue with multiple threads we have put a lock on the object’s instance.

Here is the sample code for that implementation.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Text;
    4: using Microsoft.SharePoint;
    5:  
    6: namespace EventHandlerTest
    7: {
    8:     class TestEventHandler : SPItemEventReceiver
    9:     {
   10:         private static bool IsItemUpdated = false;
   11:         object oObject = new object();
   12:  
   13:         public override void ItemAdded(SPItemEventProperties properties)
   14:         {
   15:             base.ItemAdded(properties);
   16:         }
   17:  
   18:         public override void ItemAdding(SPItemEventProperties properties)
   19:        {
   20:             base.ItemAdding(properties);
   21:         }
   22:  
   23:         public override void ItemUpdated(SPItemEventProperties properties)
   24:         {         
   25:             
   26:             lock (oObject)
   27:             {
   28:                 try
   29:                 {
   30:                     if (IsItemUpdated == true)
   31:                     {
   32:                         IsItemUpdated = false;
   33:                         return;
   34:                     }
   35:  
   36:                     if (!IsItemUpdated)
   37:                     {
   38:                         properties.ListItem.Update();
   39:                         IsItemUpdated = true;
   40:                     }
   41:  
   42:                 }
   43:                 catch (Exception ex)
   44:                 {
   45:                     IsItemUpdated = false;
   46:                 }
   47:                 finally
   48:                 {
   49:  
   50:                 } 
   51:             }           
   52:                        
   53:             
   54:         }
   55:  
   56:         public override void ItemUpdating(SPItemEventProperties properties)
   57:         {
   58:             base.ItemUpdating(properties);
   59:         }
   60:  
   61:     }
   62: }