英文:
Paging when using Microsoft.Graph.GraphServiceClient
问题
I'm using GraphServiceClient to access the Graph API. I'm not sure how to handle paging though. Let's say I run the following query which will return more than 100 records (and hence page):
var users = await client.Users
.GetAsync(rc => {
rc.QueryParameters.Select = new string[] { "userPrincipalName", "displayName", "id" };
});
In the results response I can see the OdataNextLink property, but I'm not sure how to use it. Can anyone give an example? All the documentation I've found seems to be based on using graph explorer or direct HTTP requests.
英文:
I'm using GraphServiceClient to access the Graph API. I'm not sure how to handle paging though. Let's say I run the following query which will return more than 100 records (and hence page):
var users = await client.Users
.GetAsync(rc => {
rc.QueryParameters.Select = new string[] { "userPrincipalName", "displayName", "id" };
});
In the results response I can see the OdataNextLink property, but I'm not sure how to use it. Can anyone give an example? All the documentation I've found seems to be based on using graph explorer or direct HTTP requests.
答案1
得分: 2
You need to use PageIterator
. PageIterator
has property State
which indicates whether the iteration has started or whether is completed.
在你的代码中,你需要使用 PageIterator
。PageIterator
有一个属性 State
,它用于指示迭代是否已经开始或已经完成。
In CreatePageIterator
you can specify callback action. Add the current user to a list of users.
在 CreatePageIterator
中,你可以指定回调操作。将当前用户添加到用户列表中。
Until pageIterator.State
is not Complete
you need to call pageIterator.IterateAsync()
to return data from the next page.
只要 pageIterator.State
不是 Complete
,你需要调用 pageIterator.IterateAsync()
来从下一页返回数据。
Each invoke of pageIterator.IterateAsync()
will return users for the current page.
每次调用 pageIterator.IterateAsync()
都会返回当前页的用户。
var response = await client.Users
.GetAsync(rc => {
rc.QueryParameters.Select = new string[] { "userPrincipalName", "displayName", "id" };
});
var userlist = new List
var pageIterator = PageIterator<User, UserCollectionResponse>.CreatePageIterator(client, response, (user) =>
{
userlist.Add(user);
return true;
});
await pageIterator.IterateAsync();
文档可以在这里找到(仅有简单示例)。
你可以在上述链接中找到文档(仅有简单示例)。
英文:
You need to use PageIterator
. PageIterator
has property State
which indicates whether the iteration has started or whether is completed.
In CreatePageIterator
you can specify callback action. Add the current user to a list of users.
Until pageIterator.State
is not Complete
you need to call pageIterator.IterateAsync()
to return data from next page.
Each invoke of pageIterator.IterateAsync()
will return users for the current page.
var response = await client.Users
.GetAsync(rc => {
rc.QueryParameters.Select = new string[] { "userPrincipalName", "displayName", "id" };
});
var userlist = new List<User>();
var pageIterator = PageIterator<User, UserCollectionResponse>.CreatePageIterator(client, response, (user) =>
{
userlist.Add(user);
return true;
});
await pageIterator.IterateAsync();
The documentation can be found here (only simple example)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论