Get all checked out files in SharePoint Library using CSOM

If you are in business need to find and get the list of all checked out documents from a sharepoint document library, we can easily achieve it by using client object model (csom) in C#.

The following C# code find and lists the files which are checkout by current user and all other users.

public static void GetAllCheckedOutFilesInLibrary() 
{ 
    string sitrUrl = "https://spotenant.sharepoint.com/sites/contoso"; 
    using (var ctx = new ClientContext(sitrUrl)) 
    { 
        //ctx.Credentials = 
        ctx.Load(ctx.Web, a => a.Lists); 
        ctx.ExecuteQuery(); 
 
        var listName = "Documents"; 
        List list = ctx.Web.Lists.GetByTitle(listName); 
        var items = list.GetItems( 
            new CamlQuery() {  
            ViewXml = @"<View Scope='RecursiveAll'><Query> 
            <Where><IsNotNull><FieldRef Name='File_x0020_Type' /></IsNotNull></Where> 
            </Query></View>"  
        }); 
        ctx.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser)); 
        ctx.ExecuteQuery(); 
        foreach (var item in items) 
        { 
            if (item.File.CheckOutType != CheckOutType.None) 
            { 
                Console.WriteLine("File: " + item["FileRef"].ToString().Split('/').LastOrDefault()); 
                Console.WriteLine("Checked-Out By: " + item.File.CheckedOutByUser.Title); 
                Console.WriteLine("Checked-Out User Email: " +item.File.CheckedOutByUser.Email); 
                Console.WriteLine("Last Modified: " + DateTime.Parse(item["Last_x0020_Modified"].ToString())); 
                Console.WriteLine("-----------------------"); 
                Console.WriteLine(""); 
            } 
        } 
    } 
}

In the above code first we are retrieving all the files and check every file if it is checked out or not, this method may takes some time to process large document library. So alternatively we can use filter in caml query itself and retrieve checked out files alone.

public static void GetAllCheckedOutFilesWithCamlQuery() 
{ 
    string sitrUrl = "https://spotenant.sharepoint.com/sites/contoso"; 
    using (var ctx = new ClientContext(sitrUrl)) 
    { 
        //ctx.Credentials = 
        ctx.Load(ctx.Web, a => a.Lists); 
        ctx.ExecuteQuery();  
 
        var listName = "Documents"; 
        List list = ctx.Web.Lists.GetByTitle(listName); 
        var files = list.GetItems(  
            new CamlQuery()  
            { 
            ViewXml = @"<View Scope='RecursiveAll'><Query> 
            <Where><IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull></Where> 
            </Query></View>" 
            }); 
        ctx.Load(files); 
        ctx.ExecuteQuery(); 
        foreach (var file in files) 
        { 
            Console.WriteLine("File: " + file["FileRef"].ToString().Split('/').LastOrDefault()); 
            Console.WriteLine("Checked-Out By: " + (file["CheckoutUser"] as FieldUserValue).Email); 
            Console.WriteLine("Last Modified: " + DateTime.Parse(file["Last_x0020_Modified"].ToString())); 
            Console.WriteLine("-----------------------"); 
            Console.WriteLine(""); 
        } 
    } 
} 

The above caml query retrieves documents which are checkout by all the users. If you want to get all the documents that are checked out by the current user then you can use the following caml query.

<Where>
<And>
<IsNotNull>
<FieldRef Name='CheckoutUser' />
</IsNotNull>
<Eq>
<FieldRef Name='CheckoutUser' />
<Value Type=’Integer’>
<UserID Type=’Integer’ />
</Value>
</Eq>
</And>
</Where>

Advertisement

2 thoughts on “Get all checked out files in SharePoint Library using CSOM”

Leave a Comment