Welcome to the new Box Support website. Check out all the details here on what’s changed.

Retrieving the count of items in a Folder using Java SDK (excluding items in subfolders)

Answered
New post

Comments

2 comments

  • mwiller

    Unfortunately, there's not a simple way to do this in the Java SDK today.  The Java SDK does not directly expose the raw API response for the collection of items in a folder, but instead offers an Iterable interface to get the items one by one.  The following workaround should allow you to get the count of items, but it's obviously not the most efficient:

     

    BoxFolder folder = new BoxFolder(api, folderID);
    int count = 0;
    for (BoxItem.Info item : folder) {
        count++;
    }
    System.out.println("The number of items in the folder is: " + count);

    For folders with fewer than 1,000 items in them, this should only make a single API call; otherwise, it will make one API call per 1,000 items in the folder.

    0
    Comment actions Permalink
  • jaero

    I should have clarified which method I was using to retrieve the items from the folder.

     

    The function I used to get the folder items is getChildrenRange

    which returns a PartialCollection of BoxItem.Info

    After inspecting the PartialCollection class there is a fullSize attribute which is the "size of the full collection that this partial collection is based off of."
    To verify, I made sure that fullSize returned the correct count when I manually set the limit of items returned by getChildrenRange to less than the total count of the target folder.

     

    However, mwiller's answer is still pertinent to the original question, so I accepted it as a solution.

     

    0
    Comment actions Permalink

Please sign in to leave a comment.