Retrieving the count of items in a Folder using Java SDK (excluding items in subfolders)
AnsweredLooking through the Box API reference, I noticed this JSON response from GET Folder Items
{ "total_count": 24, "entries": [ { "type": "folder", "id": "***number removed for privacy***", "sequence_id": "1", "etag": "1", "name": "Stephen Curry Three Pointers" }, { "type": "file", "id": "***number removed for privacy***", "sequence_id": "0", "etag": "0", "name": "Warriors.jpg" } ], "offset": 0, "limit": 2, "order": [ { "by": "type", "direction": "ASC" }, { "by": "name", "direction": "ASC" } ] }
And I was wondering how I could query for the `total_count` attribute using the Box Java SDK
I've also tried using BoxFolder methods such as `getInfo(BoxFolder.ALL_FIELDS)` and searching through the BoxFolder.Info object returned.
If it's not possible, that's fine but it's not made abundantly clear how to query for this in the Box Java SDK documentation and http://opensource.box.com/box-java-sdk/.
-
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.
-
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.
Please sign in to leave a comment.
Comments
2 comments