在应用中对 Microsoft Graph 数据进行分页
本文内容
由于服务器端分页或客户端分页,针对 Microsoft Graph 的某些 GET 查询会返回多页数据。 分页数据有助于提高应用的性能和 Microsoft Graph 的响应时间。
通过以下视频详细了解分页。
分页工作方式
在客户端分页中,客户端应用使用 $top 、 $skip 或 $skipToken 查询参数指定希望 Microsoft Graph 在单个页面中返回的结果数。 对客户端分页的支持,包括客户端可以在单个页面中请求的结果数取决于 API 和正在执行的查询。 例如, /users
终结点支持 $top
但不支持 $skip
。
在服务器端分页中,Microsoft Graph 服务在单个页面中返回默认的结果数,而客户端不使用 指定要返回 $top
的结果数。 例如,终结点在 GET /users
单个页面中返回默认值 100 个结果。
当需要多个查询请求来检索所有结果时,Microsoft Graph 将在响应中返回一个 @odata.nextLink
属性,该属性包含指向下一页结果的 URL。 可以通过将 @odata.nextLink
属性的 URL 值发送到 Microsoft Graph 来检索结果的下一页。 Microsoft Graph 将继续在每个响应中 @odata.nextLink
返回对 属性中下一页结果的引用,直到没有更多要检索的结果页。 若要读取所有结果,必须继续使用每个响应中返回的属性 @odata.nextLink
调用 Microsoft Graph,直到不再返回该 @odata.nextLink
属性。
例如,以下示例演示客户端分页,其中客户端使用 $top
查询参数请求租户中最多五个用户。
GET https://graph.microsoft.com/v1.0/users?$top=5
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Users.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Top = 5;
});
请阅读 SDK 文档 ,了解如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息。
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
requestTop := int32(5)
requestParameters := &graphusers.UsersRequestBuilderGetQueryParameters{
Top: &requestTop,
}
configuration := &graphusers.UsersRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters,
}
users, err := graphClient.Users().Get(context.Background(), configuration)
请阅读 SDK 文档 ,了解如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
UserCollectionResponse result = graphClient.users().get(requestConfiguration -> {
requestConfiguration.queryParameters.top = 5;
});
请阅读 SDK 文档 ,了解如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息。
const options = {
authProvider,
};
const client = Client.init(options);
let users = await client.api('/users')
.top(5)
.get();
请阅读 SDK 文档 ,了解如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\UsersRequestBuilderGetRequestConfiguration;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestConfiguration = new UsersRequestBuilderGetRequestConfiguration();
$queryParameters = UsersRequestBuilderGetRequestConfiguration::createQueryParameters();
$queryParameters->top = 5;
$requestConfiguration->queryParameters = $queryParameters;
$result = $graphServiceClient->users()->get($requestConfiguration)->wait();
请阅读 SDK 文档 ,了解如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息。
from msgraph import GraphServiceClient
from msgraph.generated.users.users_request_builder import UsersRequestBuilder
graph_client = GraphServiceClient(credentials, scopes)
query_params = UsersRequestBuilder.UsersRequestBuilderGetQueryParameters(
top = 5,
)
request_configuration = UsersRequestBuilder.UsersRequestBuilderGetRequestConfiguration(
query_parameters = query_params,
)
result = await graph_client.users.get(request_configuration = request_configuration)
请阅读 SDK 文档 ,了解如何将 SDK 添加 到项目并创建 authProvider 实例的详细信息。
如果结果包含更多结果,Microsoft Graph 将 @odata.nextLink
返回如下所示的属性以及结果的第一页:
"@odata.nextLink": "https://graph.microsoft.com/v1.0/users?$top=5&$skiptoken=RFNwdAIAAQAAAD8...AAAAAAAA"
在请求下一页结果时, @odata.nextLink
应在 属性中包含整个 URL。 根据要对其执行查询的 API,URL @odata.nextLink
值包含 $skiptoken
或 $skip
查询参数。 该 URL 还包含原始请求中存在的所有其他查询参数。 请勿尝试提取 $skiptoken
或 $skip
值并在其他请求中使用它。
分页行为因 Microsoft Graph API 不同而异。 处理分页数据时,应考虑以下几点:
结果页可能包含零个或多个结果。
不同的 API 可能具有不同的默认页面大小和最大页面大小。
如果指定超过相应 API 最大页面大小的页面大小(通过 $top
查询参数),则不同 API 的行为会有所不同。 具体取决于 API,所请求的页面大小可能会被忽略,它默认选择相应 API 的最大页面大小,否则 Microsoft Graph 会返回错误。
并非所有资源或关系都支持分页。 例如,针对 directoryRole 的查询不支持分页。 这包括读取角色对象本身和角色成员。
当对 目录资源 进行分页时,任何自定义请求标头 (不是 Authorization 或 Content-Type 标头) (如 ConsistencyLevel 标头)默认情况下不会包含在后续页面请求中。 如果需要在后续请求中发送这些标头,则必须显式设置它们。
在对目录资源 进行查询时使用$count=true
查询字符串时,@odata.count
属性仅在分页结果集的第一页中返回。
相关内容