Force checkout documents library in SharePoint online using CSOM

sravya shivapuram 211 Reputation points
2021-12-14T20:59:37.623+00:00

Hi,

I am trying to force checkout the documents present in the 'Documents' library in SharePoint Online using CSOM. Please see the code snippet below -

                        newContext.Load(newContext.Web);
                        newContext.Load(newContext.Site);    
                        List olist = newContext.Web.Lists.GetByTitle("Documents");
                        olist.ForceCheckout = true;
                        olist.Update();                        
                        newContext.ExecuteQueryAsync().Wait();

But doing so doesn't force the user to checkout documents prior to modification. What is the best way to require the documents to be checked out programmatically in C#? Any help is greatly appreciated. Thank you in advance.

Regards
SLS

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2021-12-15T02:05:06.187+00:00

    Hi @sravya shivapuram ,
    list.ForceCheckout will only set the list property to check out which means all files new added will be checked out but the files already exist still keep check in. Please refer to following code to check out exist files

    File file = ctx.Web.GetFileByServerRelativeUrl("/sites/xxx/TestFolder/test.txt");  
    file.CheckOut();  
    ctx.Load(file);  
    ctx.ExecuteQuery();  
    

    ==================
    Solution Updated==================

    I have tested the following code and set the Require Check Out property successfully.

    List olist = ctx.Web.Lists.GetByTitle("TestFolder");  
    olist.ForceCheckout = true;  
    olist.Update();  
    ctx.ExecuteQuery();  
    

    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.