Search code examples
microsoft-graph-apimicrosoft-graph-sdks

Search for users can't get it to work with ms graph java SDK


What I'm trying to do works fine through the graph explorer, and even copying the URL from my logs works too:

https://graph.microsoft.com/v1.0/users?%24search=%22displayName%3Adummy%22&%24select=id%2CdisplayName&%24orderby=displayName

Expected result:

Returns only matching users containing dummy in their display name

Actual result:

Matches all users in directory

        final OkHttpClient graphHttpClient = HttpClients.createDefault(authProvider)
                .newBuilder()
                .addInterceptor(logging)
                .build();

        DefaultLogger defaultLogger = new DefaultLogger();
        defaultLogger.setLoggingLevel(LoggerLevel.DEBUG);

        GraphServiceClient<Request> graphClient = GraphServiceClient
                .builder()
                .logger(defaultLogger)
                .httpClient(graphHttpClient)
                .buildClient();
        LinkedList<Option> requestOptions = new LinkedList<>();
        requestOptions.add(new QueryOption("$search", "\"displayName:dummy\""));
        UserCollectionPage users = graphClient.users()
                .buildRequest(requestOptions)
                .select("id,displayName")
                .orderBy("displayName")
                .get();

        users.getCurrentPage(); // contains all users in my directory, not just the searched one

Maven co-ordinates:

        <dependency>
            <groupId>com.microsoft.graph</groupId>
            <artifactId>microsoft-graph</artifactId>
            <version>3.2.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.azure</groupId>
                    <artifactId>azure-core-http-netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-core-http-okhttp</artifactId>
            <version>1.6.1</version>
        </dependency>

Solution

  • You need to add a header "ConsistencyLevel" = "eventual" to get it to work.