Paging in MS Graph API
Asked Answered
A

4

16

Graph API Paging explains that the response would contain a field @odata.nextLink which would contain a skiptoken pointing to the next page of contents.

When I test the API, I'm getting a fully-qualified MS Graph URL which contains the skiptoken as a query param. E.g. Below is the value I got for the field @odata.nextLink in the response JSON. https://graph.microsoft.com/v1.0/users?$top=25&$skiptoken=X%27445370740200001E3A757365723134406F33363561702E6F6E6D6963726F736F66742E636F6D29557365725F31363064343831382D343162382D343961372D383063642D653136636561303437343437001E3A7573657235407368616C696E692D746573742E31626F74322E696E666F29557365725F62666639356437612D333764632D343266652D386335632D373639616534303233396166B900000000000000000000%27

Is it safe to assume we'll always get the full URL and not just the skiptoken? Because if it's true, it helps avoid parsing the skiptoken and then concatenating it to the existing URL to form the full URL ourselves.

EDIT - Compared to MS Graph API, response obtained from Azure AD Graph API differs in that the JSON field @odata.nextLink contains only the skipToken and not the fully-qualified URL.

Agamic answered 22/12, 2016 at 9:45 Comment(3)
The paging link in the question seems outdated, the following provides some details: learn.microsoft.com/en-us/graph/paging?tabs=httpWillumsen
@ThorstenSchöning thank you! updated the description with the link you sharedAgamic
FYI, you can add the following query parameter at the end to increase the number of items in the result - &top=200 - reference - stack overflow question, microsoft docsGoya
D
17

Yes. In Microsoft Graph you can assume that you'll always get the fully qualified URL for the @odata.nextLink. You can simply use the next link to get the next page of results, and clients should treat the nextLink as opaque (which is described in both OData v4 and in the Microsoft REST API guidelines here: https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md#98-pagination.
This is different from AAD Graph API (which is not OData v4), which doesn't return the fully qualified next link, and means you need to do some more complicated manipulations to get the next page of results.

Hence Microsoft Graph should make this simpler for you.

Hope this helps,

Dowable answered 30/12, 2016 at 4:10 Comment(3)
Thanks! this is exactly what we need.Agamic
Is there any way we can manually create the next link URL because in my case params are having some Unicode chars and while hitting the next link it throws error.Scarfskin
How can I get the previous data? In pagination, we have to provide previous button too.Blasto
S
35

if you would like to have all users in single list, you can achieve that using the code that follows:

public static async Task<IEnumerable<User>> GetUsersAsync()
    {
        var graphClient = GetAuthenticatedClient();
        List<User> allUsers = new List<User>();
        var users = await graphClient.Users.Request().Top(998)
           .Select("displayName,mail,givenName,surname,id")
           .GetAsync();

        while (users.Count > 0)
        {
            allUsers.AddRange(users);
            if (users.NextPageRequest != null)
            {
                users = await users.NextPageRequest
                    .GetAsync();
            }
            else
            {
                break;
            }
        }
        return allUsers;
    }

I am using graph client library

Superintendent answered 5/12, 2018 at 17:58 Comment(0)
D
17

Yes. In Microsoft Graph you can assume that you'll always get the fully qualified URL for the @odata.nextLink. You can simply use the next link to get the next page of results, and clients should treat the nextLink as opaque (which is described in both OData v4 and in the Microsoft REST API guidelines here: https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md#98-pagination.
This is different from AAD Graph API (which is not OData v4), which doesn't return the fully qualified next link, and means you need to do some more complicated manipulations to get the next page of results.

Hence Microsoft Graph should make this simpler for you.

Hope this helps,

Dowable answered 30/12, 2016 at 4:10 Comment(3)
Thanks! this is exactly what we need.Agamic
Is there any way we can manually create the next link URL because in my case params are having some Unicode chars and while hitting the next link it throws error.Scarfskin
How can I get the previous data? In pagination, we have to provide previous button too.Blasto
E
6

The above code did not work for me without adding a call to 'CurrentPage' on the last line.
Sample taken from here.

        var driveItems = new List<DriveItem>();
        var driveItemsPage = await graphClient.Me.Drive.Root.Children.Request().GetAsync();
        driveItems.AddRange(driveItemsPage.CurrentPage);
        while (driveItemsPage.NextPageRequest != null)
        {
            driveItemsPage = await driveItemsPage.NextPageRequest.GetAsync();
            driveItems.AddRange(driveItemsPage.CurrentPage);
        }
Explicit answered 2/2, 2021 at 20:12 Comment(0)
B
0

I followed Tracy's answer and I was able to fetch all the messages at one go.

public List<Message> GetMessages()
{
    var messages = new List<Message>();
    
    var pages = Client.Users[_email]
                      .Messages
                      .Request(QueryOptions)
                      // Fetch the emails with attachments directly instead of downloading them later.
                      .Expand("attachments")
                      .GetAsync()
                      .Result;

    messages.AddRange(pages.CurrentPage);

    while (pages.NextPageRequest != null)
    {
        pages = pages.NextPageRequest.GetAsync().Result;
        messages.AddRange(pages.CurrentPage);
    }

    return messages;
}
Bramble answered 25/4, 2022 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.