【问题标题】:How to search the group by the DisplayName using Microsoft Graph?如何使用 Microsoft Graph 按 DisplayName 搜索组?
【发布时间】:2019-03-29 16:00:19
【问题描述】:

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

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

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

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

我收到以下错误。

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

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

【问题讨论】:

    标签: microsoft-graph-api


    【解决方案1】:

    还有一个问题:您只能指定前 21 个字符,并且搜索始终使用“startsWith”。如果您指定的不止于此,您将不走运:搜索总是失败。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    更新

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

    “搜索”功能确实有效。是一直修好还是一直修好,我不确定。

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

    第 4 点让我大吃一惊!

    您的请求将如下所示:

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

    使用请求标头: 一致性级别:最终

    【讨论】:

    • 同上^^。看起来 MS 对从响应返回的错误进行了一些改进。在 Graph Explorer 中尝试时,我得到了以下信息。 {..."message": "带有 $search 查询参数的请求仅适用于带有特殊请求标头的 MSGraph:'ConsistencyLevel: eventual'"...}
    【解决方案3】:

    这是我编写的用于使用 DisplayName 获取组的 C# 代码。此代码需要引用 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;
            }
    

    【讨论】:

      【解决方案4】:

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

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

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

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

      【讨论】:

      • 感谢您的回答。
      • 这太愚蠢了。为用户检索组的第一个用例是查找某些组,并且有 0 过滤能力。
      猜你喜欢
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多