question

WardH-0381 avatar image
0 Votes"
WardH-0381 asked WardH-0381 commented

Microsoft Graph maxing out at 1000 results in C#?

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-mailmicrosoft-graph-paging
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

GlenScales-6756 avatar image
0 Votes"
GlenScales-6756 answered WardH-0381 commented

1000 is the maximum number of items per page so you need something like this from https://docs.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi GlenScales-6756 ,

Thanks great answer.

Ward.

0 Votes 0 ·