Paging when using Microsoft.Graph.GraphServiceClient.

huangapple go评论56阅读模式
英文:

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.

在你的代码中,你需要使用 PageIteratorPageIterator 有一个属性 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 =&gt; {
                rc.QueryParameters.Select = new string[] { &quot;userPrincipalName&quot;, &quot;displayName&quot;, &quot;id&quot; };
            });

var userlist = new List&lt;User&gt;();
var pageIterator = PageIterator&lt;User, UserCollectionResponse&gt;.CreatePageIterator(client, response, (user) =&gt;
{
    userlist.Add(user);
    return true;
});

await pageIterator.IterateAsync();

The documentation can be found here (only simple example)

huangapple
  • 本文由 发表于 2023年4月13日 17:54:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定