Microsoft Graph API Pagination is not working for getting all users from Azure AD(Microsoft Graph API 分页无法从 Azure AD 获取所有用户)
问题描述
我们正在使用 Microsoft Graph API Beta 版使用以下代码从 Azure AD 检索所有用户.API 在响应中仅返回 100 个用户,为了使用分页响应,我们尝试了 NextPageRequest 属性.但它总是为 NextPageRequest 属性返回 null.因此,它永远不会进入 while 循环来检索其余用户.
We are using Microsoft Graph API Beta version to retrieve all users from the Azure AD using below code. API returns only 100 users in the response and to use paginated response we tried NextPageRequest property. But it always return null for the NextPageRequest property. And due to that it never enters to the while loop to retrieve rest of the users.
Beta SDK 版本:4.0.1.0
Beta SDK version: 4.0.1.0
代码:
List<User> usersList = new List<User>();
IGraphServiceUsersCollectionPage users = await graphClient.Users.Request().GetAsync();
// Add the first page of results to the user list
usersList.AddRange(users.CurrentPage);
// Fetch each page and add those results to the list
while (users.NextPageRequest != null)
{
users = await users.NextPageRequest.GetAsync();
usersList.AddRange(users.CurrentPage);
}
log.Info("Users count: " + usersList.Count.ToString());
return usersList;
我关注的参考链接:
- Microsoft Graph 仅返回前 100 个用户
- https://docs.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=csharp
对此的任何帮助将不胜感激!
Any help on this will be appreciated!
推荐答案
下面的代码对我来说非常好.
The below code works perfectly fine for me.
public static async Task<List<User>> getUsers()
{
List<User> usersList = new List<User>();
graphClient.BaseUrl = "https://graph.microsoft.com/beta";
IGraphServiceUsersCollectionPage users = await graphClient.Users
.Request()
.GetAsync();
usersList.AddRange(users.CurrentPage);
while (users.NextPageRequest != null)
{
users = await users.NextPageRequest.GetAsync();
usersList.AddRange(users.CurrentPage);
}
return usersList;
}
在 Azure Active Directory 用户刀片中检查您的用户,并查看其中有多少用户.您还可以通过简单地使用 $top 查询参数扩展代码来测试用户数量是否超过 100,每个请求提供 998 条记录,如下所示.
Check your users in Azure Active Directory Users Blade and see how many users are present in it. And also you can test the number of users if there are more than 100 or not by simply extending the code with $top query parameter which gives 998 records per request as shown below.
IGraphServiceUsersCollectionPage users = await graphClient.Users
.Request()
.Top(998)
.GetAsync();
您还可以在 Graph Explorer 中测试 Graph API 调用.
You can also test Graph API calls in Graph Explorer.
经过长时间的研究,我发现它是 Microsoft Graph Beta SDK 中的一个错误,因为它总是在 NextPageRequest 中发送 null.但这里有趣的是,它在 AdditionalData 属性中发送 odata.nextLink.因此,如果您使用的是 Graph Beat SDK,请使用以下代码.
After a long research I got to find out that its a bug in Microsoft Graph Beta SDK as it is always sending null in the NextPageRequest. But the interesting thing here is, it is sending the odata.nextLink in the AdditionalData property. So use the below code if you are using Graph Beat SDK.
public static async Task<List<User>> getUsers()
{
List<User> usersList = new List<User>();
IGraphServiceUsersCollectionPage users = await graphClient.Users
.Request()
.GetAsync();
usersList.AddRange(users.CurrentPage);
try
{
while (users.AdditionalData["@odata.nextLink"].ToString() != null)
{
users.InitializeNextPageRequest(graphClient, users.AdditionalData["@odata.nextLink"].ToString());
users = await users.NextPageRequest.GetAsync();
usersList.AddRange(users.CurrentPage);
}
}
catch(Exception e)
{
}
return usersList;
}
注意: Microsoft 不建议使用其 Graph Beta 版本,因为它们会发生变化.
Note: Microsoft doesn't suggest to use its Graph Beta version in Production as they are subjected to change.
这篇关于Microsoft Graph API 分页无法从 Azure AD 获取所有用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Microsoft Graph API 分页无法从 Azure AD 获取所有用户
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
