google-admin-sdkgoogle-directory-api

Google Directory API: Search for groups using a search term containing a space


I am looking to search for Groups using the Google Directory API, as per the documentation at https://developers.google.com/admin-sdk/directory/v1/guides/search-groups#fields

In particular, I am searching on the "name" field using a prefix. However, I am encountering problems when the term used to search contains a space. According to the documentation Surround with single quotes ' if the query contains whitespace, but whilst that works with the = operator, for an exact match, it does not when using the : operator, for matching the start of the text. In this instance, the API returns a "Bad Request" status code.

Here's a code sample (in C#)

    public Task<Groups> SearchGroupsAsync(DirectoryService service, string customer, string searchTerm, CancellationToken cancellationToken)
    {
        var groupsRequest = service.Groups.List();
        groupsRequest.Query = $"name:{searchTerm}*";
        groupsRequest.Customer = customer;

        return groupsRequest.ExecuteAsync(cancellationToken);
    }

The above works fine for any searchTerm that does not contain a space. As soon as a space is used in the search term, a "BadRequest" is returned.

I have all tried the following examples, and all get a BadRequest, with an error object being returned with the message Invalid Input: name:'Group *' for example

    name:Group *    
    name:'Group '*
    name:'Group *'
    name:'Group '

What does work though, is the following:

   name='Group 1'

However, I don't want to search with an exact match, just a prefix. Is it possible to search on groups with a Prefix containing a space?


Solution

  • Hi @Tim seems like we can use + character between words

    In your case we can simply use name:Group to get groups which contains Group as part of its name If you want group which matches Group 12344 , use name:Group+1*