Search code examples
graphmicrosoft-graph-apioutlook-graph-api

How to specify search option with MS graph java SDK


IMessageCollectionRequest eventRequest = graphClient.getGraphClient().users(user.getEmail()).messages()
                .buildRequest(new HeaderOption("Prefer", "outlook.body-content-type=\"text\""))
                .select("body,subject,toRecipients,ccRecipients,CreatedDateTime,conversationId,from");

IMessageCollectionPage eventPage = eventRequest
                                  .filter(filter)
                                  .get();

In the above code I am able to get results based on specified filter.

Now I want below search to be performed insteat of filter as MS graph does not support both of these to be applied.

https://graph.microsoft.com/v1.0/users/{{UserId}}/messages?$search="recipients:@xyz.com" & $top=1000

How can we specify search condition instead of filter. exactly shown in the above URL usig java SDK.


Solution

  • You can specify options in buildRequest.

    LinkedList<Option> requestOptions = new LinkedList<Option>();
    requestOptions.add(new QueryOption("$search", "\"recipients:@xyz.com\""));
    
    MessageCollectionPage messages = graphClient.users("{UserId}").messages()
        .buildRequest( requestOptions )
        .top(1000)
        .get();