如何使用 Microsoft Graph 按 DisplayName 搜索组?

Naa*_*Cat 3 microsoft-graph-api

根据文档,我可以使用以下 Graph API 列出 Office 365 组:

GET https://graph.microsoft.com/v1.0/groups

我有一个 C# Web 应用程序,并且有一个用于按 Group DisplayName 进行搜索的输入。知道如何根据 DisplayName 查询组吗?

我尝试了以下 URL:https://graph.microsoft.com/v1.0/groups?$search="displayName:Test"在 MS Graph Explorer 中,但它不起作用。

我收到以下错误。

{
"error": {
    "code": "Request_UnsupportedQuery",
    "message": "This query is not supported.",
    "innerError": {
        "request-id": "35d90412-03f3-44e7-a7a4-d33cee155101",
        "date": "2018-10-25T05:32:53"
    }
}
Run Code Online (Sandbox Code Playgroud)

欢迎任何建议。提前致谢。

小智 7

根据您的描述,我假设您想通过DisplayName使用搜索参数来搜索组。

基于此文档,我们目前只能搜索消息和人员集合。所以我们不能使用搜索参数。

我们可以使用 filter 查询参数按 DisplayName 搜索 Group。例如,我们可以搜索 displayName 以 'Test' 开头的组,请求 url 如下所示:

https://graph.microsoft.com/v1.0/groups?$filter=startswith(displayName,'Test')

  • 这太愚蠢了。为用户检索组的第一个用例是查找某些组,并且有 0 个过滤器功能。 (6认同)

小智 6

更新

我看到答案已经被接受,但我遇到了同样的问题,发现这个答案已经过时了。对于下一个人,这是更新

“搜索”功能确实有效。我不确定它是一路上固定的还是一直固定的。

  • “群组”支持搜索,
  • v1 和 beta api 都支持搜索,
  • 搜索仅适用于“displayName”和“description”字段,
  • 搜索“目录对象”需要特殊标头:“ConsistencyLevel:最终”

第 4 点让我绊倒了!

您的请求将如下所示:

https://graph.microsoft.com/v1.0/groups?$search="displayName:Test"

请求标头: ConsistencyLevel: eventual


小智 5

这是我编写的 C# 代码,用于使用 DisplayName 获取组。此代码需要对 OfficeDevPnP.Core 的引用。

private static async Task<Group> GetGroupByName(string accessToken, string groupName)
        {
            var graphClient = GraphUtility.CreateGraphClient(accessToken);

            var targetGroupCollection = await graphClient.Groups.Request()
                                        .Filter($"startsWith(displayName,'{groupName}')")
                                        .GetAsync();

            var targetGroup = targetGroupCollection.ToList().Where(g => g.DisplayName == groupName).FirstOrDefault();

            if (targetGroup != null)
                return targetGroup;

            return null;
        }
Run Code Online (Sandbox Code Playgroud)