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.
I’m sure some of you have seen this error in your SharePoint travels, but this is something that in my opinion needs to be addressed.
The WSS “Save Conflict” error is a common error that occurs when you make multiple changes to a list under the same SPWeb object without disposal. This is a pretty hairy error to catch, but it’s clear sign that you’re not being modular enough with your design. General when I see this error the first thing I think is, “It’s time to break my methods out a little better and to call my SPWeb Object when I really need it.”
One example of where this can come into play is in solutions where SPListItem field swaping is required (i.e ListItem1 swaps it’s Title field with ListItem2). For every update to a list item the SPList’s “update()” method needs to be called to save the changes. The pattern I like to follow is to retrieve the SPWeb GUID from the current site, then call a separate instance of an SPWeb object per individual list update. This is a tedious pattern and doesn’t necessarily have to be used on all occasions, but it will save you from this annoying little exception.
Here’s an example of a small method that updates a SPListItem’s fields using this pattern:
protected void itemUpdate(string FieldName, string FieldValue, SPSite site, Guid webID, Guid listID, Guid itemID)
using (SPWeb web = site.OpenWeb(webID))
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listID];
SPListItem item = list.Items[itemID];
item[FieldName] = FieldValue;
web.AllowUnsafeUpdates = false;
Response.Write(“An error occured while updating the items”);
Keep in mind, when disposing of an SPWeb object you must remember CONTEXT. If you retrieve an SPWeb object from the current context (SPContext.Current.Web), you cannot and should not dispose of this. If you dispose of the Web being used by the current context, it’s the equivalent of ripping the ground from under your feet!! If you’re doing multiple list updates the best approach would be to open and dispose of a separate instance of an SPWeb object. This will save you tons of headache in the future.
If you ever see this exception, hopefully you’ll no exactly how to handle it now and grab it by the horns!