简体   繁体   中英

Filtering group members Graph API

i'm trying to filter members of a group with GraphClient in C#, but it doesn't work. Seems like graph 1.0 now supports the filtering of group members, but i can't filter them by displayName, or specify that i need only users.

This is example from 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://learn.microsoft.com/en-us/graph/api/group-list-members?view=graph-rest-1.0&tabs=csharp the 5th example. Or am I writing the code in C# wrong?

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

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

In this case i receive bad request that ':' is not recognized after displayName.

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

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

In this i receive:

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

I'm staying 2 days on this problem and can't find a solution:|

If the search method will work for you, here is a snippet on how to do the search.

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();

Notice I escaped the double quotes. See more details on this thread . Also note that $filter is not supported for this operation.

For the request

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

You have to surround the value of QueryOption with double qoutes. Also, instead of HeaderOption you can use Header method to add a header.

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();

Try this to filter:

 List<Option> options = new List<Option>
        {
            new QueryOption("$count", "true"),
        };

        var user = await graphServiceClient.Groups[groupId].Members.Request(options).Header("ConsistencyLevel", "eventual")
         .Filter($"mail eq '{email}'").Select("id, mail")
         .GetAsync();

It´s important to use "count" and "consistencyLevel"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM