9

Using Microsoft Graph client sdk how would one go about using search Odata query to lookup if subject or body contains a certain search term.

The $search Odata query parameter is available in the Graph Client api, but i could not find how to use the parameter using the client sdk for c#.

John Staurt
  • 225
  • 3
  • 13

2 Answers2

19

You can add any query parameters by passing in a list of QueryOptions to the Request method.

List<QueryOption> options = new List<QueryOption>
{
     new QueryOption("$search", "lunch")
};
var messages = await client.Me.Messages.Request(options).GetAsync();

Documentation: https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md#custom-query-options

David
  • 2,412
  • 1
  • 14
  • 22
  • 1
    Thank you i was looking for this in the documentation, but didn't find it. – John Staurt Jul 09 '17 at 12:29
  • 1
    One more thing, when i search with "subject:pizza" or "from:help@contoso.com" as specified in the outlook mail api i get an error stating that the ":" character is not valid. – John Staurt Jul 09 '17 at 12:43
  • 1
    Just needed to surround the search term with double quotes in the string itself. I had to escape double quotes with a \ like so; string term = "\"subject:pizza\"". – John Staurt Jul 09 '17 at 16:49
  • Great! Glad you got this working, let us know if you need any more help. I'll also pass the feedback to the team that we should support a helper method for $search like we do for $filter and other OData query params so you don't have to pass in a custom query option. – David Jul 09 '17 at 18:40
  • This has been added to the documentation: https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md#custom-query-options – Caitlin Russell Jul 10 '17 at 18:26
  • Is there a possible way to change the subject of a reply email – John Staurt Jul 11 '17 at 15:16
  • @JohnStaurt would you mind asking that as a separate question with the microsoftgraph tag so all the people watching that tag get notified and can help? – David Jul 11 '17 at 17:09
2

Piggybacking off of @JohnStaurt's comments above, this worked for me:

List<QueryOption> options = new List<QueryOption>
{
     new QueryOption("$search", "\"displayName:team\" OR \"description:team\"")
};
Tracy
  • 680
  • 7
  • 16