Search code examples
c#microsoft-graph-apimicrosoft-graph-sdks

Using paging functionality of MS Graph C# SDK, am I able to grab the NextLink and resume the call with it later?


I understand that based on the documentation here that with the C# Graph SDK library I can continue requesting the next page of results until NextPageRequest == null, but I was wondering if I could save the NextLink I get back and if I encounter an error, the next time this is executed, I could resume at the place I was at using the NextLink (using the C# SDK library).

I haven't been able to find anything in the documentation around this. Is this perhaps something I could do with the RequestBuilder?


Solution

  • I figured it out, or least a way that works using the skipToken (maybe there is a nicer built in library function way of doing this).

    Example query:

    var groupMembersPaged = await graphClient.Groups[groupId]
        .Members
        .Request()
        .Top(5)
        .GetAsync();
    
    1. Extract the skipToken from the result like this:
    var skipToken = groupMembersPaged.NextPageRequest.QueryOptions
        .Where(qo => qo.Name == "$skiptoken")
        .FirstOrDefault();
    
    1. If the skipToken exists, use it in the next request:
    if (skipToken != null)
    {
        var queryOptions = new List<QueryOption>
        {
            new QueryOption("$skiptoken", skipToken.Value)
        };
    
        var groupMembersPagedSkip = await graphClient.Groups[groupId]
            .Members
            .Request(queryOptions)
            .GetAsync();
    }