我try 过它运行良好,但我只得到了100个用户.

C# Code:

    public async Task getAllUsers()
    {
        UserCollectionResponse users = await GraphClient.Users.GetAsync();

        foreach (var item in users.Value)
        {
            Console.WriteLine("item.UserPrincipalName");
        }
        Console.WriteLine(users.Value.Count);
    }

用户计数显示在最后一行

enter image description here

怎么办?

推荐答案

Note那:默认情况下,Microsoft Curve API每个请求最多返回Note个项目.通过在API请求中使用$top参数,您可以将页面大小增加到每个请求最多999个项目.您需要在请求URL中使用$top参数.

To make use of 100 parameter to your request URL, modify the code like below:

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;


namespace UserProperties
{
    public class GraphHandler
    {
        public GraphServiceClient GraphClient { get; set; }

        public GraphHandler()
        {
            var tenantId = "TenantID";
            var clientId = "ClientID";
            var clientSecret = "ClientSecret";
            GraphClient = CreateGraphClient(tenantId, clientId, clientSecret);
        }

        public GraphServiceClient CreateGraphClient(string tenantId, string clientId, string clientSecret)
        {
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };

            var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
            var scopes = new[] { "https://graph.microsoft.com/.default" };

            return new GraphServiceClient(clientSecretCredential, scopes);
        }

        public async Task getAllUsers()
        {
            try
            {
                UserCollectionResponse users = await GraphClient.Users.GetAsync((requestConfiguration) =>
                {
                    requestConfiguration.QueryParameters.Top = 999;
                });

                foreach (var item in users.Value)
                {
                    Console.WriteLine($"User Principal Name: {item.UserPrincipalName}");
                }

                Console.WriteLine($"Total users: {users.Value.Count}");
            }
            catch (ServiceException ex)
            {
                Console.WriteLine($"Error getting user details: {ex.Message}");
            }
        }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            GraphHandler handler = new GraphHandler();

            await handler.getAllUsers();
        }
    }
}

enter image description here

如果您想在每个请求中打印超过100个项目,则需要打印101属性并通过向类添加代码片段来请求下一页,类似于以下内容:

while (users?.Value != null)  
{  
foreach (var user in users.Value)  
{  
Console.WriteLine(users);  
}

// If OdataNextLink has a value, there is another page  
if (!string.IsNullOrEmpty(users.OdataNextLink))  
{  
// Pass the OdataNextLink to the WithUrl method  
// to request the next page  
users = await GraphClient.Users.WithUrl(users.OdataNextLink).GetAsync();  
}  
else  
{  
// No more results, exit loop  
break;  
}  
}

Reference:

Page through a collection using the Microsoft Graph SDKs - Microsoft Graph | Microsoft

Csharp相关问答推荐

如何使用Automapper映射两个嵌套列表

在C#中使用in修饰符

哪个nuget包含SecurityStampValidatorOptions

如何在NServicebus中配置学习传输的文件夹(NService bus 8)

WPF Windows初始化正在锁定. Net 8中分离的线程

在一个模拟上设置一个方法,该模拟具有一个参数,该参数是一个numc函数表达式

限制特定REST API不被访问,但部署代码

为什么我的表单在绑定到对象时提交空值?

如何使用C#中的主构造函数功能使用多个构造函数?

Cosmos SDK和Newtonsoft对静态只读记录的可能Mutations

Selify只更改第一个下拉菜单,然后忽略REST-C#

为什么我可以用硬编码的集合而不是变量来设置没有setter的IList属性的值?

VS 2022与VS 2019:如何/为什么创建额外的任务?

如何在C#中正确类型化带有泛型的嵌套类

将C#类导入到PowerShell

如何将 colored颜色 转换为KnownColor名称?

如何根据分割文本的块数来计算文本的大小?

无法将.Net Framework 4.8.1升级到.Net 7

无法创建&Quot;&Quot;类型的实例,因为无法将一个或多个参数绑定到

无法创建工具窗口(用于VBIDE、VBA的COM加载项扩展)