I ran into some unexpected behavior when trying to combine whitespaces in a Directory Service API search query with a wildcard character:
Directory ds = new GoogleDirectoryServiceManager().getDirectoryService("admin@randomdomain.com");
Directory.Users usersClient = ds.users();
String lastNameBuffer = term;
StringBuilder sb = new StringBuilder();
if(term.contains(" ")){
sb.append("\'");
sb.append(lastNameBuffer);
sb.append("*\'");
}else{
sb.append(lastNameBuffer);
sb.append("*");
}
queryString = "familyName:" + sb.toString();
users = usersClient.list()
.setDomain("randomdomain.com")
.setQuery(queryString)
.setMaxResults(MAX_DIRECTORY_RESULTS)
.setFields("users(name,primaryEmail,thumbnailPhotoUrl)")
.execute();
I added the single quotes to search through the directory with the spaces. Following is an extract from the Google Directory API:
Surround with single quotes ' if the query contains whitespace. Escape single quotes in queries with \', for example 'Valentine\'s Day'.
familyName: johns*
) GOODfamilyName: 'van bur*'
) NOT GOODfamilyName: 'van buren*'
) GOODIs it not possible to combine a search query with spaces with a wildcard? Or does the wildcard symbol (*
) have to be escaped?
What query do I need to use to successfully return a list of users in case #2?
During my attempts with the API explorer embedded in the documentation, I have noted that the following query, the same as you without the single quotes, would work as expected on my test domain. Can you test it ?
familyName:van bur