The best way to handle the error: the 'FileName' file was modified by SHAREPOINT \ system

advertisements

In a sharepoint project I have Event receivers that are so important,til now 7 versions of this project have deployed for clients.It's weired that one of these versions is so unstable and slow.I mean sometimes when updating an item it sticks.and this happens randomely.

NOTE: their network speed is good.surely the problem is in the project.

I checked out the code and found out the way that deals with error The file 'FileName' has been modified by SHAREPOINT\system cause a loop,sometimes.this is the code in ItemUpdated and ItemAdded event receivers to update SPListItem:

base.EventFiringEnabled = false;
bool tryAgain = false; //used to handle "The file has been modified by SHAREPOINT\system"
do
{
    tryAgain = false;
    try
    {
        try
        {
            if (_item.File.CheckOutType == SPFile.SPCheckOutType.Online || _item.File.CheckOutType == SPFile.SPCheckOutType.Offline)
                 {
                     try
                     {
                          _item.File.CheckIn("prevent checkout/locked error");
                     }
                     catch (Exception ex) { }
                 }
           _item.SystemUpdate(true);
        }
        catch (Exception ex)
        {
             throw ex;  //-- used to handle "The file has been modified by SHAREPOINT\system"
        }
    }
    catch (Exception ex) //-- used to handle "The file has been modified by SHAREPOINT\system"
    {
        if (ex.Message.ToLower().Contains("has been modified by"))
        {
            System.Threading.Thread.Sleep(1000 * 2);
            tryAgain = true;
        }
        else
        {
            this.HandleException(ex, "");
        }
     }
} while (tryAgain == true);

this while loop has these states:

1-item is updated the first time and there's no exception

2-updating cause mentioned exception and it will update after a few while iterations

3-updating cause mentioned exception and it sticks in a loop and won't update even after 200 iterations.

this issue case the project to be unstable and sometimes works so slow.what is the best way to handle this situation?


A quick search threw up a few articles that mentioned changing the event receiver definition in the elements.xml to execute synchronously instead of asynchronously (which is the default):

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="850">
      <Receiver>
        <Synchronization>Synchronous</Synchronization>
      </Receiver>
  </Receivers>
</Elements>

Article I took this from: http://onlinecoder.blogspot.co.uk/2013/07/splistitemupdate-file-has-been-modified.html