[CSOM on SharePoint Online Error] Upload & Update Items occasionally hits "File has been modified by XXX"

William Ng 0 Reputation points
2023-11-29T02:29:19.18+00:00

Hello,

I am trying to use CSOM to upload & update many files with field values to a document library on SharePoint Online.

This is what I have done on each files:

  1. Upload the file
  2. Update Modified, Modified Date, Created, Created Date ( listItem.UpdateOverwriteVersion() )
  3. Update Several fields at once like this:
//ListItem listItem
foreach(var fieldInfo in allFieldInfos)
{
	//here: parse the values into the listItem according to the field type & value etc.
	// in short, the code is something like this:
	listItem[fieldInfo.Key] = fieldInfo.Value;
	//btw I noticed that if there is Yes/No Field here, an extra update & ExecuteQuery() is needed for some reason, otherwise the field may not be updated
}
listItem.SystemUpdate();
ctx.ExecuteQuery(); // actually, I have also applied a retry on the execution here for handling 429 or 503 (throttling), but it only catches WebException

Which works fine when I upload a few hundreds of files when I am trying. However, when I try uploading thousands / ten thousands of files, sometimes the code stopped with the following error:

Microsoft.SharePoint.Client.ServerException: The file {Filename} has been modified by {user in step2} on {datetime in step2}.

Any idea on why it happens and how to prevent it? or should I just simply apply another retry on the same attempt?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,075 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,204 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 37,866 Reputation points Microsoft Vendor
    2023-11-29T07:45:44.3366667+00:00

    Hi @William Ng,

    You could apply some type of retry strategy. Please refer to following code

    public static bool TryUploadFile(ClientContext ctx, string sourceFilePath, string targetUrl, IDictionary<string, string> fileProperties)
    {
            using (var fileStream = new FileStream(sourceFilePath, FileMode.Open))
            {
                var fileInfo = new FileCreationInformation()
                {
                    ContentStream = fileStream,
                    Overwrite = true,
                    Url = Path.GetFileName(sourceFilePath)
                };
    
                var targetFolder = ctx.Web.GetFolderByServerRelativeUrl(targetUrl);
                var uploadFile = targetFolder.Files.Add(fileInfo);
                var listItem = uploadFile.ListItemAllFields;
                foreach (var p in fileProperties)
                {
                    listItem[p.Key] = p.Value;
                }
    
                return TrySaveListItem(listItem, TimeSpan.FromSeconds(2));
            }
    }
    
    
    private static bool TrySaveListItem(ListItem listItem, TimeSpan retryInterval, int retryCount = 3)
    {
            while (true)
            {
                try
                {
                    listItem.Update();
                    listItem.Context.ExecuteQuery();
                    return true; 
                }
                catch
                {
                    if (--retryCount == 0) return false;
                    Thread.Sleep(retryInterval);
                }
            }
    }
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.