0

I'm working on a set of PowerShell functions that will be used to manage our enterprise Box implementation. Right now I'm working on the groups section and can successfully return groups via the 2.0 API. However, it appears that the call to the groups API has a default limit of 100 - limiting the number of groups that are returned.

From the Box dev docs:

curl https://api.box.com/2.0/groups -H "Authorization: Bearer ACCESS_TOKEN"

returns

{
    "total_count": 1,
    "entries": [
        {
            "type": "group",
            "id": "1786931",
            "name": "friends"
        }
    ],
    "limit": 100,
    "offset": 0
}

I need to get all groups in one fell swoop, or at least have a way to batch through all the groups. Is there a way to set the limit to either unlimited (I would assume 0) or at least higher?

For some context, our first push of groups will be around 235 groups, quickly followed by another 3,000+. I'll need to update these group memberships on a regular basis (hence the PowerShell module I'm building).

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
whitwo
  • 3
  • 1
  • Do you have more than 1 group that you can see? The response to your API call indicates that you've only defined one group. If you had more, then the "total_count" would be higher. – Peter Jul 08 '14 at 16:28
  • Yes - that response example was taken straight from the Box API Docs - not what I actually see. Right now I get a limit of 100 and a total_count of 235 - which corresponds to how many groups I have in Box. – whitwo Jul 08 '14 at 17:41

1 Answers1

1

The Box API pattern for paging uses limit and offset query parameters. It's not documented for Groups, but it's worth a shot. The max limit is 1000 for other types of collections; I'd try that here and see how it goes.

curl https://api.box.com/2.0/groups?limit=1000&offset=0 
-H "Authorization: Bearer ACCESS_TOKEN"

The API doesn't support 'unlimited' queries elsewhere, so I'd assume that to be true here as well.

Update The total_count field is useful in paging. Here's some pseudo code that aggregates the Groups in the fewest possible number of API calls:

offset = 0
groups = []

do 
{
  // fetch a chunk of groups
  results = curl https://api.box.com/2.0/groups?limit=1000&offset=<offset>

  // add this chunk to your collection
  groups.add(results.entries)

  // increment the offset by the length of the chunk
  offset = offset + results.entries.length

// repeat until the number of groups you've received equals the number expected.
} while (groups.length < results.total_count)  
John Hoerr
  • 7,955
  • 2
  • 30
  • 40
  • That worked great. I was able to get all 235 groups and the limit is now set to 1000 - so I assume it will handle more groups when I have them loaded. I did try a limit of 0, which returned no results, and a limit of 10000, which returned a limit of 1000. So I guess I will be stuck with 1000 at a time. I'm assuming I can use the offset to iterate through the groups until 0 are returned. – whitwo Jul 08 '14 at 17:44
  • The `total_count` is useful here. See the update and example. – John Hoerr Jul 08 '14 at 17:56