Microsoft Graph 查询升级

微软对Microsoft Graph的查询功能进行了升级,现在可以很好地支持Azure AD对象的查询了。

在以前,如果我们想要去查找用户,可能会使用下面的URL

GET https://graph.microsoft.com/v1.0/users?$filter=startswith(displayName, 'justin')&$orderby=displayName&$select=id,displayName

但我们会收到如下的错误信息

"error": {
        "code": "Request_UnsupportedQuery",
        "message": "Sorting not supported for current query."
}

令人高兴的是现我们能够得到一个带有@odata.count属性的结果集了,当然查询的请求需要做一些小的修改。

"@odata.context": "https://graph.microsoft.com/beta/$metadata#users(id,displayName)",
"@odata.count": 2,
"value": [
   {
      "id": "4782e723-f4f4-4af3-a76e-25e3bab0d896",
      "displayName": "Justin Liu"
   },
   {
      "id": "c03e6eaa-b6ab-46d7-905b-73ec7ea1f755",
      "displayName": "Justin Li"
   }
]

要得到如上的返回内容,我们需要:

  1. 选择beta版本的终结点
  2. 在URL查询参数中增加$count=true
  3. 在请求headers中添加一个Key为ConsistencyLevel,Value为eventual的条目

Azure Active Directory存储数据的多个副本,以处理大的读量并提供高可用性。当数据更新时,更改将最终应用于所有副本。其中$search和$count查询要求客户端通过设置header的ConsistencyLevel为eventual来选择最终一致性。

下面列举一些示例查询

  • 租户中有多少用户?
GET https://graph.microsoft.com/beta/users/$count
  • 租户中有多少访客用户?
GET https://graph.microsoft.com/beta/users/?$count=true&$filter=userType eq ‘Guest’
  • 某个组中有多少成员(包括可传递成员和直接的成员)?
GET https://graph.microsoft.com/beta/groups/{group-id}/transitiveMembers/$count

可传递是什么意思呢?一个组可以包含嵌套组,那么一个组中的组里的成员,微软使用transitiveMembers链接属性去使组中组的成员为父组的可传递成员。

标记化搜索目前只支持displayNamedescription属性,它有一些搜索规则需要注意,搜索是基于这个规则,而不是完全的看是否包含要搜索的字符串。

  • 搜索显示名中带有“justin“的用户
GET https://graph.microsoft.com/beta/users?$count=true&$search="displayName:justin"
  • 搜索显示名中带有”justin“或电子邮件以admin开头的用户,以显示名排序
GET https://graph.microsoft.com/beta/users?$count=true&$search="displayName:justin" OR "mail:admin"&$orderBy=displayName
  • 搜索显示名中带有”team“的服务主体
GET https://graph.microsoft.com/beta/servicePrincipals?$count=true&$search="displayName:team"
  • 搜索一个组(包含子组)中显示名中带有”justin“的所有成员
GET https://graph.microsoft.com/beta/groups/{group-id}/transitiveMembers/microsoft.graph.user/?$count=true&$search="displayName:justin"

排序可以在筛选或查询中使用,但目前仅限于displaynameuserPrincipalName属性。

  • 获取18号楼的用户并以显示名排序
GET https://graph.microsoft.com/beta/users?$count=true&$orderby=displayName&$filter=startswith(officeLocation, '18')

最后,别忘了批量执行Graph请求的操作,使用JSON批处理

猜你喜欢

转载自blog.csdn.net/FoxDave/article/details/106355942
今日推荐