How to get the list of all users including their last login

508 Views Asked by At

I have a dashboard to do for my organization about the use of smartsheet licenses by employees. Normally, the request to obtain such information is as follows (according to the documentation):

https://api.smartsheet.com/2.0/users?include=lastLogin

However, this request displays exactly 100 results per page. All I want is to have all the results.

The documentation indicates that another parameter must be added to the request to obtain the full list of results. The request becomes:

https://api.smartsheet.com/2.0/users?include=lastLogin&includeAll=true 

It works, however I only get the entire list of users without their last login, which brings me back to my question: How to get the list of all users including their last login ?

1

There are 1 best solutions below

2
On BEST ANSWER

So, according to the docs for the List Users operation, specifying the include=lastLogin query string parameter will only include the lastLogin attribute for each User object in the response when the number of results returned in the response is 100 or fewer. (FWIW, I'd suspect that Smartsheet implemented this behavior because it'd be detrimental to API performance if a single API request tried to fetch the lastLogin value for hundreds or thousands of users.)

Good news though -- you can still get the info you want -- it's just going to take a little extra effort (and code) on your part. You'll need to submit a separate API request for each page of results (i.e., include both the include=lastLogin query string paramater and also the page query string parameter to specify the page number of results you want to retrieve with each request), and then join the results from all responses into a single list that you write to your dashboard. The value of the totalPages attribute in the initial List Users response will tell you how many total pages (of 100 users each by default, which is the maximum number of users you can request at a time if you want the lastLogin attribute included for each User object) are available -- i.e., how many requests you will need to issue in order to get all users and the lastLogin attribute for each.

For example, your first request would be:

GET https://api.smartsheet.com/2.0/users?include=lastLogin

...and let's assume that the response looks like this:

{
  "pageNumber": 1,
  "pageSize": 100,
  "totalPages": 3,
  "totalCount": 100,
  "data": [
    ...
  ]
}

This response will contain the first 100 users -- but the response tells you it's only the first page of three available pages of results (i.e., the value of the totalPages attribute in the response is 3) -- so you'll need to issue two more requests in order to have retrieved the full set of users.

So, next you issue a second request, this time including the page parameter to specify that you want page 2:

GET https://api.smartsheet.com/2.0/users?include=lastLogin&page=2

...and the response returns the second page of results that looks like this:

{
  "pageNumber": 2,
  "pageSize": 100,
  "totalPages": 3,
  "totalCount": 100,
  "data": [
    ...
  ]
}

This response will contain the next 100 users -- i.e., the second page of three available pages -- you'll need to issue one more request in order to have retrieved the full set of users.

So finally, you issue a third request, this time including the page parameter to specify that you want page 3:

GET https://api.smartsheet.com/2.0/users?include=lastLogin&page=3

...and the response contains the third (and in this example, final) page of results that looks something like this (where the value of the totalCount property is a numeric value >= 1 and <= 100 that indicates the number of users in the response):

{
  "pageNumber": 3,
  "pageSize": 100,
  "totalPages": 3,
  "totalCount": 52,
  "data": [
    ...
  ]
}

Now, because the value of the pageNumber property in the response is equal to the value of the totalPages property in the response, you know this is the final set of users and you don't need to issue any more requests.

Between the 3 requests, you've now retrieved the full set of users (and the lastLogin attribute value for each).