Performing a search request with SharePoint REST API throws a Microsoft.SharePoint.Client.UnknownError

Most of Office 365 REST APIs use the OData v4.0 protocol for exchanging and formatting data between the client and the server.

When you develop an application that uses those APIs, you need to add some HTTP headers in all your requests to let the server know that you want to use this protocol version and to specify the desired verbosity of returned metadata as you can see below :

Accept: application/json;odata.metadata=minimal
OData-Version: 4.0

But if you use these HTTP headers with the SharePoint API to perform a search by calling…

POST https://tenant.sharepoint.com/_api/Search/PostQuery

… then you will receive the following error message :

{
  "error": {
    "code":"-1,
    Microsoft.SharePoint.Client.UnknownError",
    "message":"Unknown Error"
  }
}

You could be surprised because if you try to get information about a site collection by calling…

https://tenant.sharepoint.com/_api/Site

… with the same HTTP headers, it works without any issues.

It can look weird but in fact it’s normal.

If you ever call the following URL to retrieve the OData manifest for the SharePoint API…

https://tenant.sharepoint.com/_api/$metadata

… you can see that the API only supports OData v3.0 (cf. MaxDataServiceVersion).

<edmx:Edmx Version="1.0">
  <edmx:DataServices m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0">
    ...
  </edmx:DataServices>
</edmx:Edmx>

So even if a lot of features in the SharePoint REST API are working with the OData v4.0 headers, you should better use the OData v3.0 headers such as below to avoid such issues :

Accept: application/json;odata.metadata=minimal
DataServiceVersion: 3.0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s