0

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" />

  • Ask yourself: What should happen at page.File.CheckOut(); if it is already checked out -> handle this situation in your code and you should be good to go. – helb Jun 13 '18 at 8:03
  • I took the liberty of changing the title (CSOM instead of SSOM) since the question is about the clinet-side object model API and not about the server-side. – helb Jun 13 '18 at 8:19
1

Regarding atomicity, the documentation does not explicitly say so but the CheckOut() method throws an SPException if "An error occurred when checking out the file." I would therefore assume this method executes synchronously and the file is locked after the method returns.

The CheckOut() method throws an SPException if check-out is not possible (e.g. the file is already checked out). You must handle this situation if you want to run this code in parallel.

You can either:

  • Catch the SPException and try again later.
  • Not catch (or log and re-throw) the exception and deal with it in the calling code.

Do not try to find out if the file can be checked out first. There is no guarantee that the file is checked out just before calling CheckOut().

  • So as soon as I call CheckOut(), the file is locked? What is the meaning of the Load()/ExecutreQuery calls below - I thought they make the above "really take effect"? – Jonathan Jun 14 '18 at 13:19
  • @Jonathan Well, the documentation does not explicitly say so but it states that an SPException is thrown if "An error occurred when checking out the file." I would assume that this means that the CheckOut() method is synchronous and after calling it, the file is locked. Then again, the SharePoint API documentation is not always correct or complete... I would just try it out. – helb Jun 14 '18 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.