box-apiboxapiv2

Using ItemCollection on a BoxFolder type with Box API only returns 100 results and cannot retrieve the remaining ones


For a while now, I've been using the Box API to connect Acumatica ERP to Box and everything has been going fine until recently. Whenever I try to use a BoxCollection type with the property ItemCollection, I'll only get the first 100 results no matter the limit I set in the GetInformationAsync(). Here is the code snippet:

            [PermissionSet(SecurityAction.Assert, Name = "FullTrust")]
    public BoxCollection<BoxItem> GetFolderItems(string folderId, int limit = 500, int offset = 0)
    {
        var response = new BoxCollection<BoxItem>();

            var fieldsToGet = new List<string>() { BoxItem.FieldName, BoxItem.FieldDescription, BoxItem.FieldParent, BoxItem.FieldEtag, BoxFolder.FieldItemCollection };
            response = Task.Run(() => Client.FoldersManager.GetFolderItemsAsync(folderId, limit, offset)).Result;

        return response;

    }

I then pass that information on to a BoxFolder type variable, and then try to use the ItemCollection.Entries property, but this only returns 100 results at a time, with no visible way to extract the remaining 61 (in my case, the Count = 161, but Entries = 100 always)

Another code snippet of the used variable, I am basically trying to get the folder ID based on the name of the folder inside Box:

           private static void SyncProcess(BoxFolder rootFolder, string folderName)
    {
        var boxFolder = rootFolder.ItemCollection.Entries.SingleOrDefault(ic => ic.Type == "folder" && ic.Name == folderName);
    }

I wasn't able to find anything related to that limit = 100 in the documentation and it only started to give me problems recently.

I had to create a work around by using the following:

    var boxCollection = client.GetFolderItems(rootFolder.Id);
            var boxFolder = boxCollection.Entries.SingleOrDefault(ic => ic.Type == "folder" && ic.Name == folderName);

I was just wondering if there was a better way to get the complete collection using the property ItemCollection.Entries like I used to, instead of having to fetch them again.

Thanks!


Solution

  • Box pages folder items to keep response times short. The default page size is 100 items. You must iterate through the pages to get all of the items. Here's a code snippet that'll get 100 items at a time until all items in the folder are fetched. You can request up to 1000 items at a time.

    var items = new List<BoxItem>();
    BoxCollection<BoxItem> result;
    
    do
    {
       result = await Client.FoldersManager.GetFolderItemsAsync(folderId, 100, items.Count());
       items.AddRange(result.Entries);
    } while (items.Count() < result.TotalCount);