Last active
March 5, 2022 23:21
-
-
Save kevinblumenfeld/35722efd2bbb556794f364c4f4c56963 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using DotNetCoreRazor_MSGraph.Graph; | |
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.AspNetCore.Mvc.RazorPages; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Graph; | |
using Microsoft.Identity.Web; | |
namespace DotNetCoreRazor_MSGraph.Pages | |
{ | |
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")] | |
public class EmailModel : PageModel | |
{ | |
private readonly GraphEmailClient _graphEmailClient; | |
[BindProperty(SupportsGet = true)] | |
public string NextLink { get; set; } | |
public IEnumerable<Message> Messages { get; private set; } | |
public EmailModel(GraphEmailClient graphEmailClient) | |
{ | |
_graphEmailClient = graphEmailClient; | |
} | |
public async Task OnGetAsync() | |
{ | |
var messagesPagingData = await _graphEmailClient.GetUserMessagesPage(NextLink); | |
Messages = messagesPagingData.Messages; | |
NextLink = messagesPagingData.NextLink; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Graph; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
namespace DotNetCoreRazor_MSGraph.Graph | |
{ | |
public class GraphEmailClient | |
{ | |
private readonly ILogger<GraphEmailClient> _logger; | |
private readonly GraphServiceClient _graphServiceClient; | |
public GraphEmailClient(ILogger<GraphEmailClient> logger, GraphServiceClient graphServiceClient) | |
{ | |
_logger = logger; | |
_graphServiceClient = graphServiceClient; | |
} | |
public async Task<IEnumerable<Message>> GetUserMessages() | |
{ | |
try | |
{ | |
var emails = await _graphServiceClient.Me.Messages | |
.Request() | |
.Select(msg => new | |
{ | |
msg.Subject, | |
msg.BodyPreview, | |
msg.ReceivedDateTime | |
}) | |
.OrderBy("receivedDateTime desc") | |
.Top(10) | |
.GetAsync(); | |
return emails.CurrentPage; | |
} | |
catch (Exception ex) | |
{ | |
_logger.LogError($"Error calling Graph /me/messages: {ex.Message}"); | |
throw; | |
} | |
} | |
public async Task<(IEnumerable<Message> Messages, string NextLink)> GetUserMessagesPage( | |
string nextPageLink = null, int top = 10) | |
{ | |
IUserMessagesCollectionPage pagedMessages; | |
if (nextPageLink == null) | |
{ | |
// Get initial page of messages | |
pagedMessages = await _graphServiceClient.Me.Messages | |
.Request() | |
.Select(msg => new | |
{ | |
msg.Subject, | |
msg.BodyPreview, | |
msg.ReceivedDateTime | |
}) | |
.Top(top) | |
.OrderBy("receivedDateTime desc") | |
.GetAsync(); | |
} | |
else | |
{ | |
// Use the next page request URI value to get the page of messages | |
var messagesCollectionRequest = | |
new UserMessagesCollectionRequest(nextPageLink, _graphServiceClient, null); | |
pagedMessages = await messagesCollectionRequest.GetAsync(); | |
} | |
return (Messages: pagedMessages, NextLink: GetNextLink(pagedMessages)); | |
} | |
private string GetNextLink(IUserMessagesCollectionPage pagedMessages) { | |
if (pagedMessages.NextPageRequest != null) | |
{ | |
// Get the URL for the next batch of records | |
return pagedMessages.NextPageRequest.GetHttpRequestMessage().RequestUri?.OriginalString; | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment