3

I have a site collection that has ~600 sub-sites. Each sub-site has a sub-site, as well, making a total of ~1200 sites.

I am iterating through each site and updating (resetting) a list. This is done once a day during non-working hours(100 am). 99% of the time there is no problem, but every now in then during the run, I will get the following exception:

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. ---> System.Runtime.InteropServices.COMException: Save Conflict.

I am sure there are no users on at the time of the conflict and there are no other applications that would be touching those sites.

I read one post that said I should re-get the list object before I do a List.Update(). I tried this, and it definitely cut down on the frequency of the save conflict, but I still get it now and then (about once a week). Below is my code:

         SPList list;

         list = web.Lists["Milestones"];

         for (int i = list.ItemCount - 1; i >= 0; --i)
         {
             Console.WriteLine("Deleting {0}", list.Items[i].Title);
             list.Items[i].Delete();
         }

         foreach (var milestone in project.Milestones)
         {
             Console.WriteLine("Adding {0}", milestone.Description);
             SPListItem item = list.Items.Add();
             item["LineNumber"] = milestone.LineNumber.ToString();
             item["Title"] = milestone.Description;
             item["DueDate"] = milestone.ExpectedDate.Value;
             item.Update();
         }
         try //Trying to stop save conflict
         {

             list = web.Lists["Milestones"];  //trying to stop save conflicts. 
             //Found answer that said that if updating in a loop you
             //should re-get the list object
             //Not in a loop, but thought it worth trying.
             list.Update(); //exception happens here sometimes
         }
         catch (Microsoft.SharePoint.SPException sPex)
         {
             SendEmailToEadt(string.Format(
             "Error in {0} project. {1} On List.Update {2}{3}", project.Number, 
             Environment.NewLine, Environment.NewLine, sPex.ToString()));
         }

         foreach (SPWeb childWeb in web.Webs)
         {
             UpdateMilestones(childWeb, project);
         }

My question is: Why does re-getting the list seem to reduce the save conflicts? And more importantly, since I am only doing one list.update() per site/list, how is there a save conflict at all?

2
  • 1
    Why are you calling list.Update()? Is there allways items in each list? Oct 20, 2014 at 14:26
  • There are always items added to the list, but even if not, list.update() should not be throwing a save conflict. Correct?
    – AdamBT
    Oct 20, 2014 at 14:34

1 Answer 1

2

Try adding the items to the ListItemCollection rather than adding it directly to the list, like below. The try block that you have only makes the matters worst as you are getting the list object and updating it without having anything to update.

SPList list;

     list = web.Lists["Milestones"];

     for (int i = list.ItemCount - 1; i >= 0; --i)
     {
         Console.WriteLine("Deleting {0}", list.Items[i].Title);
         list.Items[i].Delete();
     }

     foreach (var milestone in project.Milestones)
     {

        SPListItemCollection listItems = web.Lists["Milestones"].Items;
       SPListItem item = listItems.Add();

         item["LineNumber"] = milestone.LineNumber.ToString();
         item["Title"] = milestone.Description;
         item["DueDate"] = milestone.ExpectedDate.Value;

         item.Update();
     }

     try //Trying to stop save conflict
     {

         list = web.Lists["Milestones"];  //trying to stop save conflicts. 
         //Found answer that said that if updating in a loop you
         //should re-get the list object
         //Not in a loop, but thought it worth trying.
         list.Update(); //exception happens here sometimes
     }
4
  • Are you saying that the list.Update is not necessary?
    – AdamBT
    Oct 20, 2014 at 14:48
  • 1
    Yes, List.Update() is not necessary. item.Update() put changes/new item from memory to DataBase. Oct 20, 2014 at 14:51
  • 1
    yes, it is not and it is probably the reason you are getting the exception.
    – ssdar
    Oct 20, 2014 at 14:54
  • +1 it is working without the list.update. If I don't get the save conflict in the near future, I will mark your answer correct.
    – AdamBT
    Oct 20, 2014 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.