猿问

Microsoft Graph 仅返回前 100 个用户

我有下面的代码,它根据过滤器返回所有用户。问题是它只返回 100 个用户,但我知道还有更多。


private List<User> GetUsersFromGraph()

{

    if (_graphAPIConnectionDetails == null) ReadParametersFromXML();

    if (graphServiceClient == null) graphServiceClient = CreateGraphServiceClient();


    var users = graphServiceClient

        .Users

        .Request()

        .Filter(_graphAPIConnectionDetails.UserFilter)

        .Select(_graphAPIConnectionDetails.UserAttributes)

        .GetAsync()

        .Result

        .ToList<User>();


    return users;

}

该方法仅返回 100 个用户对象。我的 Azure 门户管理员报告应该接近 60,000。


皈依舞
浏览 147回答 2
2回答

慕尼黑的夜晚无繁华

Microsoft Graph 中的大多数端点以页面形式返回数据,这包括/users.为了检索您需要浏览页面的其余结果:private async Task<List<User>> GetUsersFromGraph(){&nbsp; &nbsp; if (_graphAPIConnectionDetails == null) ReadParametersFromXML();&nbsp; &nbsp; if (graphServiceClient == null) graphServiceClient = CreateGraphServiceClient();&nbsp; &nbsp; // Create a bucket to hold the users&nbsp; &nbsp; List<User> users = new List<User>();&nbsp; &nbsp; // Get the first page&nbsp; &nbsp; IGraphServiceUsersCollectionPage usersPage = await graphClient&nbsp; &nbsp; &nbsp; &nbsp; .Users&nbsp; &nbsp; &nbsp; &nbsp; .Request()&nbsp; &nbsp; &nbsp; &nbsp; .Filter("filter string")&nbsp; &nbsp; &nbsp; &nbsp; .Select("property string")&nbsp; &nbsp; &nbsp; &nbsp; .GetAsync();&nbsp; &nbsp; // Add the first page of results to the user list&nbsp; &nbsp; users.AddRange(usersPage.CurrentPage);&nbsp; &nbsp; // Fetch each page and add those results to the list&nbsp; &nbsp; while (usersPage.NextPageRequest != null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; usersPage = await usersPage.NextPageRequest.GetAsync();&nbsp; &nbsp; &nbsp; &nbsp; users.AddRange(usersPage.CurrentPage);&nbsp; &nbsp; }&nbsp; &nbsp; return users;}这里有一个非常重要的注意事项,这种方法是从 Graph(或任何 REST API)中检索数据的最不高效的方法。在下载所有这些数据时,您的应用程序将在那里停留很长时间。此处正确的方法是获取每个页面并仅处理该页面,然后再获取其他数据。

慕姐8265434

我有一个类似的用例,我的查询返回值 > 100。&nbsp; &nbsp; final GroupCollectionPage userGroups = _appClient.users({id})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .memberOfAsGroup()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .buildRequest(requestOptions)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .select("displayName,id,mail")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter("startswith(displayName, 'c')")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orderBy("displayName")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .get();所以我可以轻松地遍历结果集&nbsp; &nbsp; // Output each Group details&nbsp; &nbsp; for (Group usergroup : userGroups.getCurrentPage()) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; User Group Name: " + usergroup.displayName);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; ID: " + usergroup.id);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" Email: " + usergroup.mail);&nbsp; &nbsp; }下面介绍如何获取用户组的下一页&nbsp; public static void getGroups() {&nbsp; &nbsp; LinkedList<Option> requestOptions = new LinkedList<Option>();&nbsp; &nbsp; requestOptions.add(new HeaderOption("ConsistencyLevel", "eventual"));&nbsp; &nbsp; requestOptions.add(new QueryOption("$count", "true"));&nbsp; &nbsp; GroupCollectionPage userGroups = _appClient.users({id})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .memberOfAsGroup()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .buildRequest(requestOptions)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .select("displayName,id,mail")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter("startswith(displayName, 'c')")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orderBy("displayName")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .get();&nbsp; &nbsp; List<Group> allGroupsList = new ArrayList<>();&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; List<Group> currentPageGroup = userGroups.getCurrentPage();&nbsp; &nbsp; &nbsp; &nbsp; allGroupsList.addAll(currentPageGroup);&nbsp; &nbsp; &nbsp; &nbsp; GroupCollectionRequestBuilder nextPage = userGroups.getNextPage();&nbsp; &nbsp; &nbsp; &nbsp; userGroups = nextPage == null ? null : nextPage.buildRequest().get();&nbsp; &nbsp; } while (userGroups != null);&nbsp; &nbsp; System.out.println("Total Group count is :" + allGroupsList.size());&nbsp; &nbsp; for (Group usergroup : allGroupsList) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; User Group Name: " + usergroup.displayName);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; ID: " + usergroup.id);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("&nbsp; Email: " + usergroup.mail);&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答