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

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,300 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.
2,810 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    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.