2 回答
TA贡献1864条经验 获得超6个赞
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)中检索数据的最不高效的方法。在下载所有这些数据时,您的应用程序将在那里停留很长时间。此处正确的方法是获取每个页面并仅处理该页面,然后再获取其他数据。
TA贡献1813条经验 获得超2个赞
我有一个类似的用例,我的查询返回值 > 100。
final GroupCollectionPage userGroups = _appClient.users({id})
.memberOfAsGroup()
.buildRequest(requestOptions)
.select("displayName,id,mail")
.filter("startswith(displayName, 'c')")
.orderBy("displayName")
.get();
所以我可以轻松地遍历结果集
// Output each Group details
for (Group usergroup : userGroups.getCurrentPage()) {
System.out.println(" User Group Name: " + usergroup.displayName);
System.out.println(" ID: " + usergroup.id);
System.out.println(" Email: " + usergroup.mail);
}
下面介绍如何获取用户组的下一页
public static void getGroups() {
LinkedList<Option> requestOptions = new LinkedList<Option>();
requestOptions.add(new HeaderOption("ConsistencyLevel", "eventual"));
requestOptions.add(new QueryOption("$count", "true"));
GroupCollectionPage userGroups = _appClient.users({id})
.memberOfAsGroup()
.buildRequest(requestOptions)
.select("displayName,id,mail")
.filter("startswith(displayName, 'c')")
.orderBy("displayName")
.get();
List<Group> allGroupsList = new ArrayList<>();
do {
List<Group> currentPageGroup = userGroups.getCurrentPage();
allGroupsList.addAll(currentPageGroup);
GroupCollectionRequestBuilder nextPage = userGroups.getNextPage();
userGroups = nextPage == null ? null : nextPage.buildRequest().get();
} while (userGroups != null);
System.out.println("Total Group count is :" + allGroupsList.size());
for (Group usergroup : allGroupsList) {
System.out.println(" User Group Name: " + usergroup.displayName);
System.out.println(" ID: " + usergroup.id);
System.out.println(" Email: " + usergroup.mail);
}
}
- 2 回答
- 0 关注
- 146 浏览
添加回答
举报