OData Search with Office 365 Mail API .NET Client

2019-08-19 04:48发布

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['mail@me.com'].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['Mail@me.com'].Messages .Context.CreateQuery<Message>("Users('Mail@me.com')/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?

1条回答
干净又极端
2楼-- · 2019-08-19 05:31

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('Mail@me.com')/Messages");
var searchUri = new Uri(query.RequestUri.OriginalString + "?$search=%22subject%3Apizza%22");
var messages =  context.Execute<Message>(searchUri, "Get");
查看更多
登录 后发表回答