【问题标题】:Filtering group members Graph API过滤组成员 Graph API
【发布时间】:2021-03-25 16:48:54
【问题描述】:

我正在尝试使用 C# 中的 GraphClient 过滤组的成员,但它不起作用。似乎图 1.0 现在支持组成员的过滤,但我无法通过 displayName 过滤它们,或者指定我只需要用户。

这是来自 Microsoft 的示例:

GET https://graph.microsoft.com/v1.0/groups/{id}/members/microsoft.graph.user?$count=true&$orderby=displayName&$search="displayName:Pr"&$select=displayName,id
ConsistencyLevel: eventual

https://docs.microsoft.com/en-us/graph/api/group-list-members?view=graph-rest-1.0&tabs=csharp 第 5 个例子。还是我用 C# 写错了代码?

            List<Option> options = new List<Option>
            {
                new HeaderOption("ConsistencyLevel", "eventual"),
                new QueryOption("$search", "displayName:Pr"),
            };

            members= await _graphClient.Groups[groupId]
                .Members
                .Request(options)
                .GetAsync();
        }

在这种情况下,我收到错误请求,显示名称后无法识别 ':'。

        List<Option> options = new List<Option>
            {
                new HeaderOption("ConsistencyLevel", "eventual"),
            };

            members= await _graphClient.Groups[groupId]
                .Members
                .Request(options)
                .Filter("displayName startsWith 'pr'")
                .GetAsync();
        }

在此我收到:

Microsoft.Graph.ServiceException: Code: BadRequest
Message: Invalid filter clause

我在这个问题上待了 2 天,但找不到解决方案:|

【问题讨论】:

    标签: c# microsoft-graph-api


    【解决方案1】:

    如果搜索方法适合您,这里有一个关于如何进行搜索的 sn-p。

    List<Option> options = new List<Option>();
            options.Add(new HeaderOption("ConsistencyLevel", "eventual"));
            options.Add(new QueryOption("$search", "\"displayName:Danstan\""));
    
    var members = await graphServiceClient.Groups["group-id"].Members
                .Request(options)
                .GetAsync();
    

    请注意,我转义了双引号。在this thread 上查看更多详细信息。 另请注意,此操作不支持 $filter。

    【讨论】:

    • 返回所有成员,不按 displayName 搜索:/
    【解决方案2】:

    为了请求

    GET https://graph.microsoft.com/v1.0/groups/{id}/members/microsoft.graph.user?$count=true&$orderby=displayName&$search="displayName:Pr"&$select=displayName,id
    ConsistencyLevel: eventual
    

    您必须用双引号将QueryOption 的值括起来。此外,您可以使用Header 方法代替HeaderOption 添加标题。

    List<QueryOption> options = new List<QueryOption>
    {
         new QueryOption("$search", "\"displayName:Pr\"")
    };
    
    _graphClient.Groups[groupId].Members
    .Request()
    .Header("ConsistencyLevel", "eventual")
    .Select("displayName,id")
    .OrderBy("displayName")
    .GetAsync();
    

    【讨论】:

    • 也返回所有成员,不按displayName搜索:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多