Search code examples
c#odataoffice365office365apioffice365-restapi

OData Search with Office 365 Mail API .NET Client


I need to perform an OData query $search = "subject:pizza" using the OutLook 365 API but using the Outlookservicesclient (found the in the outlook 365 sdk, this nuget https://www.nuget.org/packages/Microsoft.Office365.OutlookServices-V2.0/)

See this OutLookAPI OData query Reference

This works correctly using an HttpClient but with the .NET client library, its seemingly not possible to add any non-standard query parameters.

Ie: var messages = await client.Users['[email protected]'].Messages .Where(m => m.IsRead == false) .Take(50) .ExecuteAsync();

Produces the following RequestURI https://outlook.office365.com/api/v2.0/Users('mail%40me.com')/Messages?$filter=IsRead eq false&$top=50 And executes correctly.

Whereas if try the following, var query = client.Users['[email protected]'].Messages .Context.CreateQuery<Message>("Users('[email protected]')/Messages") .AddQueryOption("$search", "subject:pizza");

Either returns Exception:Thrown: "Can't add query option '$search' because it begins with reserved character '$'." (System.NotSupportedException) A System.NotSupportedException was thrown: "Can't add query option '$search' because it begins with reserved character '$'."

or im getting authentication errors if I omit the AddQueryOption line.

All I need to do as append $search=subject:pizza the RequestURI! This seems impossible without actually using a rest client as the Outlook Client seems limited to built in Linq methods.

Added the fact there is no reference documentation for the client library, ive hit a dead end. Does anyone know if its possible to include $search via the outlookservicesclient?


Solution

  • I checked with the OData.NET folks, and they've opened an issue on GitHub to track the error adding $search with AddQueryOption. In the meantime, they suggested you could try something like this to make it work:

    var query = context.CreateQuery("Users('[email protected]')/Messages");
    var searchUri = new Uri(query.RequestUri.OriginalString + "?$search=%22subject%3Apizza%22");
    var messages =  context.Execute<Message>(searchUri, "Get");