Search code examples
c#.net-coremicrosoft-graph-apimicrosoft-graph-mail

Get all email message using Microsoft Graph API in c#


I have the following functions to get messages using Graph API

var client = new GraphServiceClient(authenticationProvider);
var messages = await client.Users["[email protected]"].Messages
     .Request()
     .GetAsync();

I am only able to get the latest 10 messages. How do I get all the messages? I tried to have a look at the microsoft documentation here: https://learn.microsoft.com/en-us/graph/api/message-get?view=graph-rest-1.0&tabs=csharp but unable to find any clues.


Solution

  • Found the answer after googling and trial error.

    IUserMessagesCollectionPage msgs = await _client.Users[[email protected]].Messages.Request()
                    .Filter("put your filter here")
                    .GetAsync();
                List<Message> messages = new List<Message>();
                messages.AddRange(msgs.CurrentPage);
                while (msgs.NextPageRequest != null)
                {
                    msgs = await msgs.NextPageRequest.GetAsync();
                    messages.AddRange(msgs.CurrentPage);
                }