I’ve been working with SharePoint 2007 workflows for a couple of months now, and one of the most frustrating issues I’ve encountered is the generation of a “Save Conflict” exception in workflow code.
The scenario we’ve seen is as follows:
- Your workflow code needs to update a field on the list item it is running against.
- You make the change to the field required and called the Update method of the item.
- An exception is thrown with the following message:
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.
Now clearly this message seems more aligned with an “interactive” SharePoint user session given it’s advice to “click Back in your Web browser“, but it’s being generated by a programmatic action. Searching the web there are a number of common approaches suggested to address this issue:
- Simply catch the exception, throw it away and try the Update again which should / may succeed.
- Try using the SystemUpdate method which accoring to the MSDN documentation “Updates the database with changes that are made to the list item without effecting changes in the modified time or modified by fields, or optionally, the item version.“
- Simply catch the exception, and throw it away …
We tried all three approaches, and although unpalatable, Option 3 was the one that worked best for us. Options 1 and 2 did not work reliably in our experience. The changes are saved successfully, and we haven’t experienced any issues.
I should say however that a lot of the examples have code pretty much like the following:
try
{
item.Update();
}
catch {}
Now that’s all well and good as long as the only exception that could ever be thrown at this point in time is the “Save Conflict” exception, but obviously that’s not the case, so we’ve adopted the following general approach:
try
{
item.Update();
}
catch (SPException spEx)
{
if (!spEx.Message.ToLower().Contains("save conflict"))
{
throw spEx;
}
}
In this way, should an exception (including a non-SharePoint exception) other than the “Save Conflict” error, we simply re-throw and our surround exception handling takes care of it and logs it.
But the question remains, although simply catching and discarding the exception seems very bad practice, why is the exception being thrown in the first place? Is it a bug? Is it due to mismatched identities under which the workflows are running? I’d love to hear from others on their experiences of this very frustrating circumstance.
July 14th 2008 – For the next installment of my findings regarding this exception, and SharePoint state machine workflows, check out my later post The-delay-activity-is-your-friend.
Leave a reply to SPEventReceiverType.CheckingIn « SharePoint 2007 Hut Cancel reply