Microsoft Graph only returning the first 100 Users(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 个用户
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
