My GraphEmailClient method for retrieving pages of messages from a specific folder looks like this:
{
//this is because the ids sometimes end with equals signs, which get truncated in a querystring
MailFolderId = MailFolderId.TrimEnd('ÿ').TrimStart('ÿ');
IMailFolderMessagesCollectionPage pagedMessages;
try
{
if (nextPageLink == null)
{
// Get initial page of messages
pagedMessages = await _graphServiceClient.Me.MailFolders[MailFolderId].Messages
.Request()
.Select(msg => new
{
msg.Subject,
msg.BodyPreview,
msg.ReceivedDateTime,
msg.From,
msg.IsRead,
msg.HasAttachments
})
.Top(top)
.OrderBy("receivedDateTime desc")
.GetAsync();
}
else
{
// Use the next page request URI value to get the page of messages
var messagesCollectionRequest = new MailFolderMessagesCollectionRequest(nextPageLink, _graphServiceClient, null);
pagedMessages = await messagesCollectionRequest.GetAsync();
}
}
catch (Exception ex)
{
_logger.LogError($"Error calling Graph /me/messages to page messages: {ex.Message}");
throw;
}
return (Messages: pagedMessages,
NextLink: GetNextLink(pagedMessages));
}
The OnGetAsync
routine in my codebehind looks like this:
public async Task OnGetAsync(string mailfolder)
{
var messagesPagingData = await _graphEmailClient.GetMailFolderMessagesPage(mailfolder, NextLink);
FolderId = mailfolder;
Messages = messagesPagingData.Messages;
NextLink = messagesPagingData.NextLink;
await Task.CompletedTask;
}
And finally, my Next button in the Razor page looks like this:
@if (!String.IsNullOrEmpty(Model.NextLink)) {
<a asp-page="/EmailPreview?mailfolder=@fixedfolder&[email protected]" asp-route-nextlink="@Model.NextLink" class="btn btn-primary" style="width:270px">Next Page</a>
}
When I omit the querystrings on the asp-page
, the NextLink
value comes through correctly, because of the asp-route-nextlink
. But the folderid
does not. It shows up as null, even if I put this on the page:
<input type="hidden" name="folderid" id="folderid" value="@Model.FolderId" />
Which is why I got rid of that line and added folderid
into the asp-page
address. But that stopped NextLink
from coming through. I seem to be able to get one or the other, but not both.
All of this works fine if I use IUserMessagesCollectionPage
instead of IMailFolderMessagesCollectionPage
, and do everything without specifying a mail folder. But I need to specify a folder, so that doesn’t help me.