【问题标题】:Microsoft Graph only returning the first 100 UsersMicrosoft Graph 仅返回前 100 个用户
【发布时间】:2020-08-17 15:53:02
【问题描述】:

我有以下代码,它根据过滤器返回所有用户。问题是它只返回 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。

【问题讨论】:

    标签: c# microsoft-graph-api microsoft-graph-sdks


    【解决方案1】:

    Microsoft Graph 中的大多数终结点都以页面形式返回数据,其中包括 /users

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

    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)中检索数据的性能最低的方法。在下载所有这些数据时,您的应用程序将在那里停留很长时间。此处正确的方法是获取每个页面并在获取其他数据之前仅处理该页面

    【讨论】:

    • 我同意。这是一天一次的批处理工作的一部分,所以我想我会没事的。但是,如果我想在用户操作的应用程序上使用此代码,我确实同意您的观点。那就有问题了。
    • @Marc LaFleur,我正在使用这种方法,但正如你所说,这需要大量时间......但我需要以某种方式将用户放在列表中,因为当我去创建一个合作者我有 azure AD 用户的下拉列表,我选择他们来创建具有该信息的用户...我怎样才能以最佳性能实现这一目标?
    • 如果您有足够多的用户,这会花费大量时间,那么他们可能不应该出现在下拉列表中。最好使用过滤用户列表的搜索框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多