Microsoft Graph仅返回前100个用户 [英] Microsoft Graph only returning the first 100 Users

查看:84
本文介绍了Microsoft Graph仅返回前100个用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the below code which returns all users based on a filter. The problem is it only returns 100 users but I know there are a lot more.

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.

the method returns only 100 user objects. My Azure portal admin reports there should be closer to 60,000.

推荐答案

Microsoft Graph中的大多数端点返回页面中的数据,其中包括/users.

Most of the endpoints in Microsoft Graph return data in pages, this includes /users.

为了检索其余结果,您需要浏览以下页面:

In order to retrieve the rest of the results you need to look through the pages:

private async Task<List<User>> GetUsersFromGraph()
{
    if (_graphAPIConnectionDetails == null) ReadParametersFromXML();
    if (graphServiceClient == null) graphServiceClient = CreateGraphServiceClient();

    // Create a bucket to hold the users
    List<User> users = new List<User>();

    // Get the first page
    IGraphServiceUsersCollectionPage usersPage = await graphClient
        .Users
        .Request()
        .Filter("filter string")
        .Select("property string")
        .GetAsync();

    // Add the first page of results to the user list
    users.AddRange(usersPage.CurrentPage);

    // Fetch each page and add those results to the list
    while (usersPage.NextPageRequest != null)
    {
        usersPage = await usersPage.NextPageRequest.GetAsync();
        users.AddRange(usersPage.CurrentPage);
    }

    return users;
}

这里有一个非常重要的说明,该方法是从Graph(或实际上是任何REST API)检索数据的最有效的方法.您的应用程序在下载所有这些数据时会坐在那里很长时间.此处的正确方法是获取每个页面并在获取其他数据之前先处理该页面.

One super important note here, this method is the lest performant way to retrieve data from Graph (or any REST API really). Your app will be sitting there for a long time while it downloads all this data. The proper methodology here is to fetch each page and process just that page before fetching additional data.

这篇关于Microsoft Graph仅返回前100个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆