Microsoft Graph 客户端 - 检索超过 15 个用户?

Microsoft Graph client - retrieve more than 15 users?

我正在使用 Microsoft Graph 客户端并希望根据 objectId 列表检索用户。到目前为止,我已经设法做到了:

// Create filterstring based on objectId string list.
var filterString = string.Join(" or ", objectIds.Where(x => !string.IsNullOrEmpty(x)).Select(objectId => $"id eq '{objectId}'"));

// Get users by filter
var users = await _graphServiceClient.Users.Request()
               .Select(x => new { x.UserPrincipalName, x.Id })
               .Filter(filterString)
               .GetAsync(ct).ConfigureAwait(false);

但是我在这里遇到了这个错误:

Too many child clauses specified in search filter expression containing 'OR' operators: 22. Max allowed: 15.

有没有其他方法只获取一部分用户?还是我需要将列表“分块”成 15 个?

您可能应该拆分查询并将 BATCH 请求发送到 Graph API。 这将只向服务器发送 1 个请求,但允许您一次查询更多数据。

https://docs.microsoft.com/en-us/graph/sdks/batch-requests?tabs=csharp

这可能看起来像这样:(未经测试的代码)

var objectIds = new string[0];
var batchRequestContent = new BatchRequestContent();
var requestList = new List<string>();

for (var i = 0; i < objectIds.Count(); i += 15)
{
    var batchObjectIds = objectIds.Where(x => !string.IsNullOrEmpty(x)).Skip(i).Take(15);
    var filterString = string.Join(" or ", batchObjectIds.Select(objectId => $"id eq '{objectId}'"));

    var request = _graphServiceClient.Users.Request()
        .Select(x => new { x.UserPrincipalName, x.Id })
        .Filter(filterString);

    var requestId = batchRequestContent.AddBatchRequestStep(request);
    requestList.Add(requestId);
}

var batchResponse = await _graphServiceClient.Batch.Request().PostAsync(batchRequestContent);

var allUsers = new List<User>();

foreach (var it in requestList)
{
    var users = await batchResponse.GetResponseByIdAsync<GraphServiceUsersCollectionResponse>(it);

    allUsers.AddRange(users.Value.CurrentPage);
}