I have a C# CSOM code that updates a page based on its current context, code sample below.
Now, I want to run several instances of this code. I'm concerned that the instances would overwrite each other: If 2 instances read the page and update their stuff, then one instance's update would overwrite the other's.
Do the checkout/checkin protect me again that? What would happen if both instances do it in the same time?
void UploadToSharepoint(string SharepointFolder, string pageName)
{
using (var context = new ClientContext(SharepointFolder))
{
// handle context.Credentials
var pagesList = context.Web.Lists.GetByTitle("Pages");
context.Load(pagesList);
context.ExecuteQuery();
var pageQuery = new CamlQuery()
{
ViewXml = $@"<View><Query><Where>
<Eq>
<FieldRef Name='FileLeafRef'/>
<Value Type='Text'>{pageName}</Value>
</Eq>
</Where></Query><RowLimit>100</RowLimit></View>",
};
var pages = pagesList.GetItems(pageQuery);
context.Load(pages);
context.ExecuteQuery();
ListItem page = pages.Single();
page.File.CheckOut();
string pageContent = page["PublishingPageContent"];
page["PublishingPageContent"] = pageContent;
page.Update();
page.File.CheckIn(comment: "", checkInType: CheckinType.MajorCheckIn);
page.File.Publish(comment: "");
context.Load(page);
context.ExecuteQuery();
}
}
I use: <package id="Microsoft.SharePointOnline.CSOM" version="16.1.6518.1200" />
page.File.CheckOut();
if it is already checked out -> handle this situation in your code and you should be good to go.