英文:
Microsoft Graph API bulk presence lookup
问题
以下是翻译好的部分:
我正在使用以下代码来检索多个用户的在线状态:
var presences = await _graphServiceClient.Communications
.GetPresenceByUserIds(ids)
.Request
.PostAsync();
是否有类似的请求可以批量获取这些用户的 IDs?我不得不一个一个地查找它们,这在整个请求中会导致一些延迟,特别是如果我需要获取大约 20 个用户的在线状态。
编辑以补充:
获取用户的 IDs 的代码如下:
currentUser = await _graphServiceClient.Users[email]
.Request()
.Select(u => new
{
u.Id
})
.GetAsync();
return currentUser.Id;
因此,我查询每个电子邮件地址并将结果的 ID 添加到列表/数组中。在选择过滤器中添加 u.Presence
返回一个空的 Presence。
英文:
I am using the following code to retrieve multiple user presences:
var presences = await _graphServiceClient.Communications
.GetPresenceByUserIds(ids)
.Request
.PostAsync();
Is there a similar request to get the ids in bulk? I am having to look them up one by one and this is causing a bit of a delay in the whole request - especially if I need ~20 presences
Edited to add:
The code the get users' ids is:
currentUser = await _graphServiceClient.Users[email]
.Request()
.Select(u => new
{
u.Id
})
.GetAsync();
return currentUser.Id;
So I query each email and append the result id to a list / array. Adding u.Presence in the select filter returns a null Presence.
答案1
得分: 1
你可以使用 userPrincipalName
和 in
运算符来筛选用户。限制是你只能以这种方式查询15个用户。
var queryOptions = new List<Option>
{
new HeaderOption("ConsistencyLevel","eventual"),
new QueryOption("$count", "true")
};
var userIds = await client.Users
.Request(queryOptions)
.Select("id")
.Filter("userPrincipalName in ('user1@xxx.com',...,'user15@xxx.com')")
.GetAsync();
这将减少调用的数量,但仍然不止一次调用。
但是,上面的查询可以用于批量请求:
POST https://graph.microsoft.com/v1.0/$batch
{
"requests": [
{
"url": "/users?$filter=userPrincipalName in ('user1@xxx.com',...,'user15@xxx.com')&$select=id&$count=true",
"method": "GET",
"id": 1,
"headers": {
"ConsistencyLevel": "eventual"
}
},
{
"url": "/users?$filter=userPrincipalName in ('user16@xxx.com',...,'user30@xxx.com')&$select=id&$count=true",
"method": "GET",
"id": 2,
"headers": {
"ConsistencyLevel": "eventual"
}
}
]
}
在批量请求中,你可以定义最多20个请求,因此你可以获取约300个用户的 id。
英文:
You can filter users by userPrincipalName
and using in
operator. The limitation is that you can query only 15 users this way.
var queryOptions = new List<Option>
{
new HeaderOption("ConsistencyLevel","eventual"),
new QueryOption("$count", "true")
};
var userIds = await client.Users
.Request(queryOptions)
.Select("id")
.Filter("userPrincipalName in ('user1@xxx.com',...,'user15@xxx.com')")
.GetAsync();
It will reduce the number of calls but it isn't still one call.
But the query above can be used in a batch request
POST https://graph.microsoft.com/v1.0/$batch
{
"requests": [
{
"url": "/users?$filter=userPrincipalName in ('user1@xxx.com',...,'user15@xxx.com')&$select=id&$count=true",
"method": "GET",
"id": 1,
"headers": {
"ConsistencyLevel": "eventual"
}
},
{
"url": "/users?$filter=userPrincipalName in ('user16@xxx.com',...,'user30@xxx.com')&$select=id&$count=true",
"method": "GET",
"id": 2,
"headers": {
"ConsistencyLevel": "eventual"
}
}
]
}
C# code:
var queryOptions = new List<Option>
{
new HeaderOption("ConsistencyLevel","eventual"),
new QueryOption("$count", "true")
};
var batchRequestContent = new BatchRequestContent();
var requestMessage1 = client.Users
.Request(queryOptions)
.Select("id")
.Filter("userPrincipalName in ('user1@xxx.com',...,'user15@xxx.com')")
.GetHttpRequestMessage();
batchRequestContent.AddBatchRequestStep(requestMessage1);
var requestMessage2 = client.Users
.Request(queryOptions)
.Select("id")
.Filter("userPrincipalName in ('user16@xxx.com',...,'user30@xxx.com')")
.GetHttpRequestMessage();
batchRequestContent.AddBatchRequestStep(requestMessage2);
var batchResponseContent = await client.Batch.Request().PostAsync(batchRequestContent);
In the batch, you can define up to 20 requests, so you can get id of ~300 users.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论