Thursday, October 4, 2012

Error while customizing item added event receiver

Following error is generated at the time of updating an item in item added event receiver.

"System.Runtime.
InteropServices.COMException: 0x81020015The file PublishingImages/sample.jpg has been modified by..."

This type of issue will occur for following libraries:
  • Document library
  • Form library
  • Wiki page library
  • Picture Library
  • Reports library
  • Calendar list
 Following is the code to fix this issue:

public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);
           string strWebId = properties.Web.Url;
           string strListName = properties.List.Title;
           int intListId = properties.ListItemId;

           using (SPSite objSite = new SPSite(strWebId))
           {
               using (SPWeb objWeb = objSite.OpenWeb())
               {
                   EventFiringEnabled = false;
                   objWeb.AllowUnsafeUpdates = true;
                   SPList objList = objWeb.Lists.TryGetList(strListName);
                   SPListItem item = objList.GetItemById(intListId);
                   if (objList.ItemCount == 0)
                   {
                       item["Chapter"] = 1;
                   }
                   else
                   {
                       //Get max course value

                   }
                   item.SystemUpdate(false);
                   EventFiringEnabled = true;
                   objWeb.AllowUnsafeUpdates = false;
               }
           }
       }

Reference: http://sharepointconnoisseur.blogspot.in/2011/04/save-conflict-error-when-creating-list.html