Jason Shave
Jason Shave

Reputation: 2672

Microsoft Graph Client SDK -filter groups by name

A rather simple ask but I'm having trouble converting my basic httpClient method of querying the Graph into the SDK method. I was using the following and it works fine:

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

...now I'm attempting to filter using the SDK as follows:

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

I've also tried .Filter($"startswith("displayName", {filter})) and other variants.

I'm getting an invalid filter clause error. Any ideas?

Upvotes: 5

Views: 5811

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59338

Apparently it occurs since the provided filter expression for Filter method is invalid, it could be validated like this:

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

The generated message.RequestUri will return the following value:

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

A valid filter expression needs to be specified like this:

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

In case if you want to switch to beta version for GraphServiceClient class, it could be specified like this:

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

Upvotes: 7

Related Questions