Microsoft Graph maxing out at 1000 results in C#?

WardH 61 Reputation points
2021-08-14T05:04:59.317+00:00

Hi,

I am using Microsoft Graph but I am confused why I only get back 1000 results (I know there is more data) when I specify a number higher then 1000.

Any thoughts on how to fix? I only get back 1000 there is over 12000 in my result set. I was expecting based on my code to get 5000.

Here is my code below.

Thanks,

Ward

            var messages = await graphClient.Users[email_address].MailFolders.Inbox.Messages
                .Request()
                .Select("receivedDateTime")
                .Top(5000)
                .OrderBy("receivedDateTime desc")
                .Filter("receivedDateTime ge 2020-06-30T14:00:00Z AND receivedDateTime lt 2021-06-30T14:00:00Z")
                .GetAsync();
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,521 questions
0 comments No comments
{count} votes

Accepted answer
  1. Glen Scales 4,431 Reputation points
    2021-08-16T00:27:48.81+00:00

    1000 is the maximum number of items per page so you need something like this from https://learn.microsoft.com/en-us/graph/sdks/paging?tabs=csharp

    var messages = await graphClient.Me.Messages  
        .Request()  
        .Header("Prefer", "outlook.body-content-type=\"text\"")  
        .Select(e => new {  
            e.Sender,  
            e.Subject,  
            e.Body  
        })  
        .Top(1000)  
        .GetAsync();  
      
    var pageIterator = PageIterator<Message>  
        .CreatePageIterator(  
            graphClient,  
            messages,  
            // Callback executed for each item in  
            // the collection  
            (m) =>  
            {  
                Console.WriteLine(m.Subject);  
                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();  
    

    This will enumerate all the items in a the Folder so if you want to stop after 5000 you need to to put some logic to break after the number has been reached if you look at the second example on the docs page that gives an example of that.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful