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)
{
try
{
using (SPWeb web = site.OpenWeb(webID))
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listID];
SPListItem item = list.Items[itemID];
item[FieldName] = FieldValue;
item.Update();
list.Update();
web.AllowUnsafeUpdates = false;
}
}
catch(Exception ex)
{
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!
~:)
7 comments:
Awesome post! So helpful. Thanks!
Very helpful!
Nice one!
I'm seeing this error but I'm using SP Web Services! No SPWeb to dispose of. The error suggests that I refresh the page, but how to do that programmatically? Weird.
Try Creating your own SPWeb Object and use the URL for the site you're trying to work with. That might end up being a little more work though.
If you are facing this error in document library and checkout is needed to update property or set default value of column and then follow this to do
http://urenjoy.blogspot.com/2008/09/set-default-value-of-column-in-document.html
Hi,
your solution is working, but it looked to heavy to me (opening the web site each time you need to update something..)
so, I came up with another solution, which is to reload your SPListItem using the following syntax:
item = SPContext.Current.Web.Lists[item.ParentList.ID].Items[item.UniqueId];
For example, after you add a new group to the web site and called web.Update(), you will need to reload your item if you got a reference to it before adding your group.
So what exactly happens when you dispose of the SPWeb object if it was obtained using the SPContext object? If the update to the item has already happened, what's the harm of disposing?
Post a Comment