Tuesday, December 22, 2009

The file has been modified by SHAREPOINT\system

Sometimes your project architecture includes multiple solutions like using Event Handlers , Timer Jobs , workflows ....
You need all these componets to update a certain ListItem so imagine the following case :

Inside your workflow you get a reference to your working list item using
SPListItem cItem = WorkflowProperties.List.GetItemByID(WorkflowProperties.ItemID);

Then you Made some modifications
cItem["My Field1"]="PlaPla";
cItem["My Field2"]="PlaPla";
cItem["My Field3"]="PlaPla";
.
.
.
then Another code in the Task

then you called
cItem.Update();

and this problem has been occured
The file has been modified by SHAREPOINT\system

Cause
After you get a reference to the SPListItem and before you call the Update() method another component modified the List Item so SharePoint tells you that you have an old version of the file and there may be a conflict exists.

Solution
Put your updates inside a while loop and catch this exception if it occured try making your updates again but getting the current version of the ListItem

Code Sample

SPListItem req = CurrentRequest;
bool tryAgain = false;
do
{
try
{
req["field1"] = "value1";
req["field2"] = "value2";
req.Update();
tryAgain = false;
}
catch (Exception ex)
{
WriteToLog(ex.StackTrace, ex.Message);
if (ex.Message.Contains("has been modified by SHAREPOINT"))
{
req = CurrentRequest;
System.Threading.Thread.Sleep(1000 * 5);
tryAgain = true;
}
}
} while (tryAgain == true);


While CurrentRequest is a private proberty to get a new ListItem at every call.

private SPListItem CurrentRequest
{
get
{
SPList list = _wfp.Web.Lists[_wfp.ListId];
SPListItem req = list.GetItemById(_wfp.ItemId);
return req;
}
}



Thanks