I have created an Office Add In that is used to upload a Word document to SharePoint, showing a list of sites/subsites and Document Libraries. After the file is uploaded I set the content type based on what is selected in the UI. The code is as follows:
using(var vs = new FileStream(documentLocation, FileMode.Open, FileAccess.Read))
{
var fileInfo = new FileCreationInformation
{
ContentStream = fs,
Url = serverRelativeFileUrl,
Overwrite = false
};
uploadFile = documentLibrary.RootFolder.Files.Add(fileInfo);
context.ExecuteQuery();
ListItem listItem = file.ListItemAllFields;
context.Load(listItem);
context.ExecuteQuery();
listItem["ContentTypeId"] = contentTypeId;
listItem.Update();
context.ExecuteQuery();
}
Occasionally this code fails saying that the file is locked for shared use has been modified by my user account. It seems to fail on the last ExecuteQuery() call. It is not consistent and works most of the time. Is there something wrong with the code I have, or how can I prevent this error from occurring?
Edit The error message actually says: The file has been modified by my account name...not that it is locked for shared use.
context.Load(listItem);context.ExecuteQuery();