3
votes

According to the document, I can list the Office 365 Groups by using the following Graph API:

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

I have a C# Web application, and there is a input for searching by the Group DisplayName. Any idea how to query groups based on the DisplayName?

I have tried the following URL: https://graph.microsoft.com/v1.0/groups?$search="displayName:Test" in the MS Graph Explorer which didn't work.

I get the following error.

{
"error": {
    "code": "Request_UnsupportedQuery",
    "message": "This query is not supported.",
    "innerError": {
        "request-id": "35d90412-03f3-44e7-a7a4-d33cee155101",
        "date": "2018-10-25T05:32:53"
    }
}

Any suggestion is welcomed. Thanks in advance.

3

3 Answers

7
votes

According to your description, I assume you want to search the Group by the DisplayName using the search parameters.

Based on this document, we can currently search only message and person collections. So we couldn't use the search parameter.

We can use the filter query parameter to search the Group by DisplayName. For example, we can search the groups whose displayName is start with 'Test',the request url like this:

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

5
votes

Here is C# code that I wrote to get a group using the DisplayName. This code requires a reference to the 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;
        }
2
votes

UPDATE

I see that the answer has already been accepted, but I came across the same issue and found that this answer is out of date. For the next person, this is the update:

The 'search' functionality does work. Whether it was fixed along the way or always has, I am not sure.

  • 'groups' support search,
  • both the v1 and beta api support search,
  • search only works on 'displayName' and 'description' fields,
  • searching on 'directory objects' require a special header: 'ConsistencyLevel: eventual'

Point number 4 is what tripped me up!

Your request would look like this:

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

With the request header: ConsistencyLevel: eventual