How to implement pagination using Okta.Sdk in .net core app?

It would be really helpful if someone share the examples as well.

Which endpoint are you looking to make requests to?

The management SDKs methods are designed to handle pagination for you, so that you can fetch the complete list of users/groups/etc for your request, see note for the ListUsers method in our documentation.

I was looking at the ListUsers endpoint. I am trying to get the Links through SDKs method but seems like limit param is not working for me. I am always getting all records even if I apply the limit 10.

for ex :- await _oktaClient.Users.ListUsers(q: query, after: null, limit: 10, filter: null).ToListAsync();

The above code part is returning all the records at once instead of 10 per page. Am I missing something here?

I believe when limit param works, then able to see the links info in response.

You’re right, the limit determines the number of results to be returned per page in the requests the SDK makes the Okta. But the SDK will continue making API requests to page through all of the result of users that fulfill your query, which it sounds like you are seeing. You should be able to confirm by checking the network events that are being made from your application to Okta to see that multiple calls are occurring (presuming, of course, that you have more than 10 users in your org and thus have more than one page of users to be returned).

Are you trying to only get 10 users back instead of all users that match your query?

1 Like

Yes, I was trying to get the 10 users per page by using limit parameter. And, I am able to achieve it by using code :- okta.Users.ListUsers(after: after, limit: limit, search: searchParam).GetPagedEnumerator(); Though, when processing the records it provides me the self page cursor and the next page cursor. I am wondering if we have an option to get back to the previous page using before param through Okta dotnet sdk, which I am not able to find at the moment.

var enumerator = _okta.Users.ListUsers(after: after, limit: limit, search: searchParam).GetPagedEnumerator();
        if (await enumerator.MoveNextAsync())
        {
            pagedResponse.Users = _mapper.Map<IList<UserResponse>>(enumerator.CurrentPage.Items);
            pagedResponse.After = GetTokenValue<string>(enumerator?.CurrentPage?.NextLink?.Target, "after=(\\w*)");
        }

Sharing this code as I implemented this to get the next page cursor for now. Do we have any way through sdk to go back to the previous page?

As the SDK is using the link response header to provide the self page cursor and next page cursor, there is nothing built into the SDK that can provide you with a cursor to the previous page.

Of course, you could look into storing these cursors yourself as each page of results is fetched, that way you have the cursor for each page and can go back to any one of them