My requirement is to get the children item count of Document Library using CSOM. The count should only be the immediate children count, and should not include the sub-children count. I am trying to use below code to achieve this:

var newObjClientContext = this.GetSharePointClientContext(accessToken, fullUri);
WebCollection collWeb = newObjClientContext.Web.GetSubwebsForCurrentUser(new SubwebQuery());
var documentLibrary = newObjClientContext.Web.Lists.GetById(docID);

ListItemCollection ltitems = null;

string vquery = @"<View >
                                <FieldRef Name='FSObjType' /> 
                                <Value Type='Lookup'>1</Value>
                                <FieldRef Name='FSObjType' /> 
                                <Value Type='Lookup'>0</Value>
                        <OrderBy><FieldRef Name='FileLeafRef' Ascending='TRUE'></FieldRef></OrderBy>                                        
                    <RowLimit>" + recCount + @"</RowLimit>

CamlQuery camlQuery = new CamlQuery();

camlQuery.ViewXml = vquery;
ltitems = documentLibrary.GetItems(camlQuery);
newObjClientContext.Load(ltitems, lists => lists.IncludeWithDefaultProperties(l => l.ParentList));


int totalcount = documentLibrary.ItemCount; //It includes count of all the items present at all levels.

Can anyone suggest how can I get children count in above step?

  • 1
    Try with ltitems.Count instead Commented Jul 27, 2017 at 13:58

2 Answers 2


I'm not sure what you are doing in your CAML - most probably you are looking for ltitems.Count.

You could also work with the folders in the library like this:

using (var ctx = new ClientContext(...))
    var web = ctx.Web;
    var lib = web.Lists.GetByTitle("Documents");
    Console.WriteLine("ItemCount on library: {0}", lib.ItemCount);
    var root = lib.RootFolder;
    ctx.Load(root, f=>f.ItemCount, f => f.Folders);
    Console.WriteLine("items in root: {0}", root.ItemCount);
    Console.WriteLine("folders in root: {0}", root.Folders.Count());

    var folder = root.Folders.Single(f => f.Name == "Test");
    ctx.Load(folder, f => f.ItemCount, f => f.Folders);
    Console.WriteLine("items in {0}: {1}", folder.Name, folder.ItemCount);
    Console.WriteLine("folders in {0}: {1}", folder.Name, folder.Folders.Count());    

Keep in mind that the folder-count in the root-folder is alway one more than you "see" in the UI. (The one called "Attachements".)

  • I am trying to get the pages set of results from a document library. The result set is to include the folders and files only, hence that CAML query. To display the navigation on my website, I need total count of document library. Commented Jul 28, 2017 at 7:08
  • While the Folder count will be 1 more, root.ItemCount should give the right count count. Correct? Commented Jul 28, 2017 at 7:22

Removing the 'View' tag from the CAML Query and leaving 'Query' only will query just the root items. However, it will only work if the amount of items (all children and sub children) within the collection is less than 5000. Also, the CAML query returns a value in the list item collection, in your case: 'ltitems', so the count should not be attempted on the 'documentLibrary' but on 'ltitems'.

  • I don't want to restrict my result set in terms of the count (as you say max is 5000). Secondly, I am trying to get paged set of result, so count on list item collection will return the collection count and not the total count. Commented Jul 28, 2017 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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