1

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 >
                <Query>
                    <Where> 
                        <Or> 
                            <Eq>
                                <FieldRef Name='FSObjType' /> 
                                <Value Type='Lookup'>1</Value>
                            </Eq> 
                            <Eq>
                                <FieldRef Name='FSObjType' /> 
                                <Value Type='Lookup'>0</Value>
                            </Eq> 
                            </Or> 
                        </Where>
                        <OrderBy><FieldRef Name='FileLeafRef' Ascending='TRUE'></FieldRef></OrderBy>                                        
                    </Query>
                    <RowLimit>" + recCount + @"</RowLimit>
                    </View>";

CamlQuery camlQuery = new CamlQuery();

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

newObjClientContext.ExecuteQuery();

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 – Robert Lindgren Jul 27 '17 at 13:58
0

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");
    ctx.Load(lib);
    ctx.ExecuteQuery();
    Console.WriteLine("ItemCount on library: {0}", lib.ItemCount);
    var root = lib.RootFolder;
    ctx.Load(root, f=>f.ItemCount, f => f.Folders);
    ctx.ExecuteQuery();
    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);
    ctx.ExecuteQuery();
    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. – Saket Jul 28 '17 at 7:08
  • While the Folder count will be 1 more, root.ItemCount should give the right count count. Correct? – Saket Jul 28 '17 at 7:22
0

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. – Saket Jul 28 '17 at 7:12

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.