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?