4

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.

1
  • not the answer I am looking for. I have put a c# code not a URL one. Commented Oct 21, 2020 at 22:31

4 Answers 4

7

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);
            }
Sign up to request clarification or add additional context in comments.

2 Comments

I believe this is the correct answer with one caveat. It needs to save the state of msgs or will be stuck in an infinite loop. needs to be msgs = await msgs.NextPageRequest.GetAsync();
And how do you filter? what is the syntax for it? I can't find it. Thanks.
2

You can do it with .Top():

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

Comments

0

I think you should refer to this document:

Depending on the page size and mailbox data, getting messages from a mailbox can incur multiple requests. The default page size is 10 messages. To get the next page of messages, simply apply the entire URL returned in @odata.nextLink to the next get-messages request. This URL includes any query parameters you may have specified in the initial request.

Comments

0

There is now a PageIterator included in the Microsoft Graph SDK that can be used to eager load all pages.

var messages = await graphClient.Me.Messages
    .GetAsync(requestConfiguration =>
    {
        requestConfiguration.QueryParameters.Top = 10;
        requestConfiguration.QueryParameters.Select =
            ["sender", "subject", "body"];
        requestConfiguration.Headers.Add(
            "Prefer", "outlook.body-content-type=\"text\"");
    });

if (messages == null)
{
    return;
}

var pageIterator = PageIterator<Message, MessageCollectionResponse>
    .CreatePageIterator(
        graphClient,
        messages,
        // Callback executed for each item in
        // the collection
        (msg) =>
        {
            Console.WriteLine(msg.Subject);
            return true;
        },
        // Used to configure subsequent page
        // requests
        (req) =>
        {
            // Re-add the header to subsequent requests
            req.Headers.Add("Prefer", "outlook.body-content-type=\"text\"");
            return req;
        });

await pageIterator.IterateAsync();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.