Microsoft Graph api code in C# displays only limited number of users

后端 未结 2 681
眼角桃花
眼角桃花 2020-12-12 04:35

I am running below Microsoft Graph Api code:

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Net.Http;
using System.Net.Ht         


        
相关标签:
2条回答
  • 2020-12-12 04:52

    Something like this will get all of your users. Also if you want properties outside of the default you need need to specify them with a select. Not all properties are returned by default.

                String Properties = "Comma Separated List of Properties You actaully Need";
                List<User> AllUsers = new List<User>();
                IGraphServiceUsersCollectionPage users = graphServiceClient.Users
                                                            .Request()
                                                            .Select(Properties)
                                                            .GetAsync()
                                                            .Result;
    
                do
                {
                    QueryIncomplete = false ;
                    AllUsers.AddRange(users);
                    if (users.NextPageRequest != null)
                    {
                        users = users.NextPageRequest.GetAsync().Result;
                        QueryIncomplete = true;
                    }
    
                }while (QueryIncomplete);
    
                return AllUsers;
    
    0 讨论(0)
  • 2020-12-12 05:00

    Most Microsoft Graph endpoints return paged result sets. Your initial request only returns the first page of data. To retrieve the next page, you follow the URI provided in the @odata.nextLink property. Each subsequent page will return the next page's @odata.nextLink until you the last page of data (denoted by the lack of a @odata.nextLink in the result). There is a step-by-step walkthrough of how this works at Paging Microsoft Graph data in your app.

    The single most important tip I can give you here is to not use $top to force it to return large pages of data. This is an extremely inefficient method for calling the API and inevitably leads to network errors and request throttling. It also doesn't eliminate the need to handle paging since even $top=999 (the maximum) can still return multiple pages.

    Implement paging, keep your page sizes small, and process the results after each page is returned before moving on to the next page. This will ensure you capture all of the data and allow your application to pick up where it left off should it encounter any errors during processing.

    0 讨论(0)
提交回复
热议问题