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

时间:2018-10-25 06:16:47

标签: microsoft-graph

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

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

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

我尝试了以下URL:在MS Graph Explorer中无法运行的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"
    }
}

任何建议都值得欢迎。 预先感谢。

3 个答案:

答案 0 :(得分:1)

根据您的描述,假设您要使用搜索参数按DisplayName来搜索组。

基于this document,我们当前只能搜索消息和人员集合。因此我们无法使用搜索参数。

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

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

答案 1 :(得分:1)

更新

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

“搜索”功能确实有效。我不确定它是沿途修复的还是一直修复的。

  • 'groups' 支持搜索,
  • v1 和 beta api 都支持搜索,
  • 搜索仅适用于“显示名称”和“描述”字段,
  • 搜索“目录对象”需要一个特殊的标题:“ConsistencyLevel: eventual”

第 4 点让我绊倒了!

您的请求如下所示:

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

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

答案 2 :(得分:0)

这是我编写的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;
        }