May 2, 2019

SharePoint: spHttpClient search query returns HTTP 500 when requesting Created field

Problem

When making SharePoint search query programmatically via REST API and including Created in selectProperties, query returns HTTP 500.

1
2
3
4
this.webPartContext.spHttpClient.get(
  `${this._searchUrl}?querytext='${query}'&selectProperties='Title,Path,Created'`,
  SPHttpClient.configurations.v1
)

When making the query via browser, it works nicely with and without Created in selectProperties.

Solution

Include specific ISPHttpOptions in the get call makes it work as described below.

In any case, it is good idea to include the odata=nometadata option in your REST calls to enable JSON Light whenever applicable to minimize the return payload. Usually you’re not interested in the metadata anyway.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// define _noMetaOpt
private _nometaOpt: ISPHttpClientOptions = {
  headers: { 'Accept': 'application/json;odata=nometadata', 'odata-version': '' }
};

// and call
this.webPartContext.spHttpClient.get(
  `${this._searchUrl}?querytext='${query}'&selectProperties='Title,Path,Created'`,
  SPHttpClient.configurations.v1,
  this._nometaOpt
)

No comments:

Post a Comment