首页 > 解决方案 > Microsoft Graph 客户端 SDK - 按名称过滤组

问题描述

一个相当简单的问题,但我无法将httpClient查询 Graph 的基本方法转换为 SDK 方法。我正在使用以下,它工作正常:

    var filter = "IT";
    var response = await httpClient.GetAsync($"{webOptions.GraphApiUrl}/beta/groups?$filter=startswith(displayName, '{filter}')&$select=id,displayName");

...现在我正在尝试使用 SDK 进行过滤,如下所示:

    var groups = await graphServiceClient.Groups
        .Request()
        .Filter($"displayName startswith {filter}")
        .Select("id, displayName")
        .GetAsync();

我也尝试过.Filter($"startswith("displayName", {filter}))其他变体。

我收到一个invalid filter clause错误。有任何想法吗?

标签: microsoft-graph-api

解决方案


显然它发生是因为为Filter方法提供的过滤器表达式无效,它可以像这样验证:

var message = graphServiceClient.Groups
        .Request()
        .Filter($"displayName startswith '{filter}'")
        .Select("id, displayName").GetHttpRequestMessage();

生成的message.RequestUri将返回以下值:

https://graph.microsoft.com/v1.0/groups?$filter=displayName startswith '{filter}'&$select=id, displayName}

需要像这样指定有效的过滤器表达式:

.Filter($"startswith(displayName, '{filter}')")

如果您想切换到类的beta版本GraphServiceClient,可以这样指定:

graphServiceClient.BaseUrl = "https://graph.microsoft.com/beta";  

推荐阅读