6

I use GraphServiceClient to get messages from Outlook.

var messages = await _graphServiceClient.Me.MailFolders[folderId].Messages.Request()
    .Header("Prefer", "outlook.body-content-type=\"text\"")
    .Filter($"createdDateTime gt {greaterDate} and createdDateTime lt {lessDate}")
    .Top(int.MaxValue).GetAsync();

Request gets max 1000 items.

I want to get other messages and follow documentation.

My code:

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

await pageIterator.IterateAsync();

But nothing happened when I call my code. It looks like after pageIterator.IterateAsync(); it's infinity loop.

What is wrong?

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
Oleg Sh
  • 8,496
  • 17
  • 89
  • 159

1 Answers1

5

I found the limitation for list messages with Top query. The following saying is from list message api.

Depending on the page size and mailbox data, getting messages from a mailbox can incur multiple requests. The default page size is 10 messages. Use $top to customize the page size, within the range of 1 and 1000.

Description from OData Top parameter.

The minimum value of $top is 1 and the maximum depends on the corresponding API.

=================================================

.Top(int.MaxValue) I'm afraid you'd better to set the value to 999 since I'm not sure if it has a limit for the max value for it. And I also test the Iterator in my side and it really took much time, so I add a count like the document showed.

public async Task<string> mailsAsync() {
            //var mail = await _graphServiceClient.Me.Messages.Request().GetAsync();
            int count = 0;
            var messages = await _graphServiceClient.Me.Messages
                                .Request()
                                .Header("Prefer", "outlook.body-content-type=\"text\"")
                                .Select(e => new {
                                    e.Sender,
                                    e.Subject,
                                    e.Body
                                })
                                .Top(300)
                                .GetAsync();

            var pageIterator = PageIterator<Message>
                .CreatePageIterator(
                    _graphServiceClient,
                    messages,
                    (m) =>
                    {
                        Console.WriteLine(m.Subject);
                        count++;
                        return true;
                    },
                    // Used to configure subsequent page
                    // requests
                    (req) =>
                    {
                        // Re-add the header to subsequent requests
                        req.Header("Prefer", "outlook.body-content-type=\"text\"");
                        return req;
                    }
                );

            await pageIterator.IterateAsync();
            count = count + 1;
            return "success";
        }

enter image description here enter image description here

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
  • Does it work with the filter: .Filter($"createdDateTime gt {greaterDate} and createdDateTime lt {lessDate}") ? The query in the question contains also filter. – user2250152 May 02 '22 at 05:22
  • 2
    I tested it just now, addinga filter and it worked as well.. – Tiny Wang May 02 '22 at 05:28
  • AFAIK the value part within a filter query should always be set in single quotes. So if you got trouble try `createdDateTime gt '2021-01-31'`. – Oliver May 02 '22 at 05:56