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();
}
}
}
如果您想在每个请求中打印超过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