Tuesday, April 26, 2011

“Save Conflict" error when creating list or document library with "List Added Event Handler"

After adding "List Added Event Handler" to our SharePoint 2010 feature to update the list properties after listed created, we run into major issue during any list or document library creation by choosing below templates. The issue is we are getting "Save conflict" unexpected error. It is not consistent but happens much more frequently on “Calendar list”. The following list template will have such issue.
  • Document library
  • Form library
  • Wiki page library
  • Picture Library
  • Reports library
  • Calendar list


Users will get the error as indicated int he screen shot.

The error log is identical to the error people reported.

Microsoft.SharePoint.SPException: Save Conflict
Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.



This is a very typical error occurs in SharePoint 2007 for asynchronous event handler. Due to SharePoint and the event receiver code trying to update the item at the same time. After debugging this issue and working with Microsoft, we have finally resolved it on SharePoint 2010. Here are the three changes you should try.

1.  Disable Event Firing when update the list. Please note this.EnableEventFiring() has been deprecated and the code for SharePoint 2010 is:
     EventFiringEnabled = false; 
     // Business logic here  
     EventFiringEnabled = true;

2. Use new SPWeb object for each list modification. The code should look like this if you apply #1 and #2 roles.

try
{
       // Use new SPWeb object for each list modification.
       SPWeb web = null;
       base.ListAdded(properties);

       //Disable Event Firing when update the list
       EventFiringEnabled = false;  
       properties.List.ParentWeb.AllowUnsafeUpdates = true;
       web = properties.Web.Site.OpenWeb(properties.Web.ID);
       SPList list = properties.List;
       list.Title = "Correlation Id- Save Conflict error";
       // Other actions to add policy to the list
       // Action to add tagging to the content
       list.Update();
       properties.List.ParentWeb.AllowUnsafeUpdates = false;
       EventFiringEnabled = true;
}


3. If you still have save conflict issues like we did, in SharePoint 2010, we have the option to make these kind of events Synchronous by adding this in the Element.xml file of the event receiver while we are registering it.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers >
      <Receiver>
        <Name>MyCompanyListReceiverListAdded</Name>
        <Type>ListAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>mycompany.com.sp.features.mycompanyListReceiver.mycompanyListReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
        <Synchronization>Synchronous</Synchronization>
      </Receiver>
  </Receivers>
</Elements>


After doing adding these changes, we are able to resolve the issue and enjoy the list event.

6 comments:

  1. That's going to be really useful. Thanks for your post Harry. Cheers. Howard

    ReplyDelete
  2. I followed all the above but we still have the same problem when using ItemAdded. Any ideas what could be causing this?

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

    ReplyDelete
  3. Thanks! I found this helpful for my issues with SP2010. One catch .. When calling openWeb() we should show a Dispose or a using statement right?

    ReplyDelete
  4. This worked!!!! Thank you for the post!

    ReplyDelete
  5. Thanks a lot harry for nicely documenting. It fixed the issue for me :)

    ReplyDelete
  6. Even though we applied first and third option it is not working as expected. Still I am getting Save Conflict error in Sharepoint 2010. Please suggest guide us on this.

    ReplyDelete