4

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

1 Answers1

5

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

  • 1
    Thank you for the speedy response.

    This has resolved it.

    Just to explain why I didn't spot it. I was so pleased that the new framework handled things like content type headers without me doing anything that I hadn't appreciated that it was always setting ODATA version and when I investigated it I noticed that it was automatically choosing between version 3 and version 4 I assumed you had also taken care of all of this.

    Can I suggest a change so that if the API URL includes /_api/search/ you automatically remove the odata-version header.

    – Sebastian Rogers Aug 22 '16 at 12:29
  • Sounds reasonable. It's being tracked here - https://github.com/SharePoint/sp-dev-docs/issues/44 – PatMill_MSFT Aug 22 '16 at 18:49