How to handle pagination with Platform SDK/.net

Hello,

I am using the Platform SDK/.Net.

What methodology should I use to handle paginated results for API call results?

Reading posts and examining documentation, I have seen a number of different approaches.

I have been relying on nextUri being provided. I have discovered in some instances nextUri isn't incldued in the response. Using the API explorer I can demonstrate this with GET /api/v2/routing/skills and GET /api/v2/flows/datatables (output shown below). In this example I have explicitly defined the page number and made the page size 2 so that results below are minimal (mentioning this because it might seem strange). In the GET datatables response, no nextUri is present.

I suppose I could also just increase the page number until I don't see anything return in entities. I wanted to get feedback from others on how they have handled this.

GET /api/v2/routing/skills?pageSize=2&pageNumber=1 HTTP/1.1
Host: api.mypurecloud.com
Authorization: Bearer *******************
Content-Type: application/json
{
  "entities": [
    {
      "id": "[redacted]",
      "name": "[redacted]",
      "dateModified": "2023-02-23T06:54:59Z",
      "state": "active",
      "version": "1",
      "selfUri": "/api/v2/routing/skills/[redacted]"
    },
    {
      "id": "[redacted]",
      "name": "[redacted]",
      "dateModified": "2023-02-23T06:55:44Z",
      "state": "active",
      "version": "1",
      "selfUri": "/api/v2/routing/skills/[redacted]"
    }
  ],
  "pageSize": 2,
  "pageNumber": 1,
  "total": 182,
  "lastUri": "/api/v2/routing/skills?pageSize=2&pageNumber=91",
  "firstUri": "/api/v2/routing/skills?pageSize=2&pageNumber=1",
  "selfUri": "/api/v2/routing/skills?pageSize=2&pageNumber=1",
  "nextUri": "/api/v2/routing/skills?pageSize=2&pageNumber=2",
  "pageCount": 91
}
GET /api/v2/flows/datatables?pageNumber=1&pageSize=2 HTTP/1.1
Host: api.mypurecloud.com
Authorization: Bearer *******************
Content-Type: application/json
{
  "entities": [
    {
      "id": "[redacted]",
      "name": "[redacted]",
      "division": {
        "id": "[redacted]",
        "name": "Home",
        "selfUri": "/api/v2/authorization/divisions/[redacted]"
      },
      "selfUri": "/api/v2/flows/datatables/[redacted]"
    },
    {
      "id": "[redacted]",
      "name": "[redacted]",
      "division": {
        "id": "[redacted]",
        "name": "Home",
        "selfUri": "/api/v2/authorization/divisions/[redacted]"
      },
      "description": "[redacted]",
      "selfUri": "/api/v2/flows/datatables/[redacted]"
    }
  ],
  "pageSize": 2,
  "pageNumber": 1,
  "total": 163,
  "pageCount": 82
}

I use a generic function to iterate most of them and convert them into a consolidated list locally;


      list.AddRange(entity.Entities); // Add to the local List 
      // We have to cast PageCount to an int? because the dynamic recasts it to an int somehow and we have to confirm it has a value before we act on it.
      if (((int?)entity.PageCount).HasValue && entity.PageCount > 1) // Let's get paging.
      {
        int pageSize = entity.PageSize; // in case the API lowers the answer, we have to adjust
        int pageCount = entity.PageCount;
        for (int page = 2; page <= pageCount; page++)
        {
          Thread.Sleep(1000);
          entity = method.Invoke(api, new object[] { pageSize, page });
          list.AddRange(entity.Entities); // Append
        }
      }
      return list;

But you can see it's basically run the Get once, if it has a PageCount higher than one ask for the next until we get through the last page, per suggestion #1

You can see in your own example you could expect 91 pages at that pageSize.

More APIs seem to support that way than the NextUri method.

Thanks, will use this methodology going forward.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.