How to supply a querytext parameter to the Search REST API without receiving an 500 (Internal Server Error) in return

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/191340

  •  11-10-2020
  •  | 
  •  

Question

I have successfully got the SharePoint framework https://github.com/SharePoint/sp-dev-docs/wiki working and am now trying to use it to do a Search via the REST api.

If I do not specify a queryText parameter in the URL then I get a 200 OK response with an empty search result set.

return this.context.httpClient.get(
  this.context.pageContext.web.absoluteUrl + `/_api/search/query`)
  .then((response: Response) => {
    return response.json();
  });

If I append a queryText parameter

return this.context.httpClient.get(
  this.context.pageContext.web.absoluteUrl + `/_api/search/query?querytext='workbench'`)
  .then((response: Response) => {
    return response.json();
  });

I get an error object returned

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

The console shows:

sp-client-framework.bundle_82745e2….js:6 
GET https://xxx.sharepoint.com/sites/dev/_api/search/query?querytext=%27workbench%27 
500 (Internal Server Error)

Using the query directly in the browser or via the SharePoint Search Query Tool v2.5 correctly returns the search results.

What do I need to do to get it working in the SharePoint framework?

Cheers

Sebastian

Était-ce utile?

La solution

This is a known issue. The HttpClient adds an odata-version header to all calls. That is not compatible with the Search API endpoint. You need to add a blank value to that header, as follows

this._httpClient
  .get(this._webAbsoluteUrl + `/_api/search/query?querytext='*'`, {
    headers: {
      "odata-version": ""
    }
  })

For more information see this issue on Github: https://github.com/SharePoint/sp-dev-docs/issues/44

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top