微软对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"
}
]
要得到如上的返回内容,我们需要:
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链接属性去使组中组的成员为父组的可传递成员。
标记化搜索目前只支持displayName和description属性,它有一些搜索规则需要注意,搜索是基于这个规则,而不是完全的看是否包含要搜索的字符串。
GET https://graph.microsoft.com/beta/users?$count=true&$search="displayName:justin"
GET https://graph.microsoft.com/beta/users?$count=true&$search="displayName:justin" OR "mail:admin"&$orderBy=displayName
GET https://graph.microsoft.com/beta/servicePrincipals?$count=true&$search="displayName:team"
GET https://graph.microsoft.com/beta/groups/{group-id}/transitiveMembers/microsoft.graph.user/?$count=true&$search="displayName:justin"
排序可以在筛选或查询中使用,但目前仅限于displayname和userPrincipalName属性。
GET https://graph.microsoft.com/beta/users?$count=true&$orderby=displayName&$filter=startswith(officeLocation, '18')
最后,别忘了批量执行Graph请求的操作,使用JSON批处理。