question

DanWahlin-6308 avatar image
0 Votes"
DanWahlin-6308 asked saldana-msft edited

Using nextLink to retrieve email messages with the Microsoft Graph .NET Core SDK

What is the best practice way to use nextLink with the .NET Core SDK and Microsoft Graph when working with email messages? I'm currently doing the following to retrieve messages and then move forward through them:

 public async Task<(IEnumerable<Message> Messages, int Skip)> GetUserMessagesPage(int pageSize, int skip = 0)
 {
     var pagedMessages = await _graphServiceClient.Me.Messages
             .Request()
             .Select(msg => new
             {
                 msg.Subject,
                 msg.Body,
                 msg.BodyPreview,
                 msg.ReceivedDateTime
             })
             .Top(pageSize)
             .Skip(skip)
             .OrderBy("receivedDateTime")
             .GetAsync();
       
     var skipValue = pagedMessages
         .NextPageRequest?
         .QueryOptions?
         .FirstOrDefault(
             x => string.Equals("$skip", WebUtility.UrlDecode(x.Name), StringComparison.InvariantCultureIgnoreCase))?
         .Value ?? "0";
    
     _logger.LogInformation($"skipValue: {skipValue}");
    
     return (Messages: pagedMessages, Skip: int.Parse(skipValue));
 }


While this works fine, I'd prefer to use the value in @odata.nextLink. I tried doing something like the following but get an error about the token not being sent when SendAsync() is called (the nextLink value is hard-coded in this simple example but I can retrieve it from pagedMesssages.AdditionalData):

 var httpRequest = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/messages?%24select=subject%2cbody%2cbodyPreview%2creceivedDateTime&%24orderby=receivedDateTime&%24top=5&%24skip=5");
 var response = await _graphServiceClient.HttpProvider.SendAsync(httpRequest);

The _graphServiceClient object is the same one used with the first code shown above which works perfectly fine. It's injected automatically (full code at https://github.com/DanWahlin/DotnetCoreRazor-MicrosoftGraph/blob/main/Startup.cs). But, going through the _graphServiceClient.HttpProvider doesn't seem to do what I was expecting given that no token is sent. What's the best way to use nextLink to call and get the next set of messages with the .NET Core SDK?

microsoft-graph-sdkmicrosoft-graph-mailmicrosoft-graph-paging
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey, are you able to provide more information about the error you are getting?

I cloned your app from the url and uncommenting the lines the added here resulted in a successful http 200 response.


0 Votes 0 ·

Thanks for taking a look Andrew. I really appreciate it.

I'm completely baffled by the fact that it works now. I just ran it again and I get a 200 now too. The only change I made since posting this question and trying it is updates to the assembly versions but I wouldn't think that would change that....maybe it did though. Regardless, the "token not sent" message no longer shows so it looks like I can move on with this solution. Thanks again!

1 Vote 1 ·

Glad to hear it all worked out!

0 Votes 0 ·

1 Answer

DanWahlin-6308 avatar image
1 Vote"
DanWahlin-6308 answered DanWahlin-6308 edited

Ended up going with the following (thanks to Andrew's recommendation):

 UserMessagesCollectionRequest messagesCollectionRequest = 
    new UserMessagesCollectionRequest(nextPageLink, _graphServiceClient, null);
 var pagedMessages = await messagesCollectionRequest.GetAsync();


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.