c# - Microsoft Graph 仅返回前 100 个用户

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

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

最佳答案

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

关于c# - Microsoft Graph 仅返回前 100 个用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56707404/

相关文章:

azure - Graph API 无法按用于登录的电话号码筛选 Azure AD B2B 用户

从 API 调用 Microsoft Graph 时 Azure AD B2C 重置密码选项

microsoft-graph-api - 图生命周期通知未注册正确的端点

asp.net-core - 当 Web 应用程序对 Microsoft Graph 的访问者及其自身进行身份验证时的身份验证流程

office365 - 创建群组日历事件失败

python-3.x - 使用 Python 和服务主体通过 Microsoft Graph API 发送电子邮件

c# - nhibernate queryover.where 未将对象引用设置为对象的实例

c# - 查找 Windows 是否是没有 WMI 的 OEM

c# - 使用 linq 从正则表达式匹配中获取组名

c# - 从 DirectoryInfo.GetFiles 返回的 FileInfo 没有指向文件?