Microsoft Graph 仅返回前 100 个用户

Microsoft Graph only returning the first 100 Users(Microsoft Graph 仅返回前 100 个用户)

本文介绍了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 个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Microsoft Graph 仅返回前 100 个用户

基础教程推荐