简体   繁体   中英

GraphServiceClient and get additional data

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?

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";
        }

在此处输入图像描述 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM