Use SharePoint Framework and Search API

%3CLINGO-SUB%20id%3D%22lingo-sub-9996%22%20slang%3D%22en-US%22%3EUse%20SharePoint%20Framework%20and%20Search%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-9996%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20build%20a%20search-driven%20app%20using%20the%20new%20SharePoint%20Framework.%20Whenever%20I%20use%20the%20built-in%20HTTP%20client%2C%20I%20get%20a%20500%20error%20for%20search.%20The%20same%20GET%20request%20runs%20fine%20in%20the%20same%20browser%20session.%20I've%20run%20through%20SP%20framework%20tutorials%20on%20GitHub%20and%20the%20list%20requests%20work%20fine%20(in%20the%20same%20app)%20but%20search%20continues%20to%20return%20a%20500%20error.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAny%20pointers%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere's%20a%20sample%20of%20my%20function%20trying%20to%20call%20search...%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Eprotected%20get%20searchHttpClientOptions()%3A%20IHttpClientOptions%20%7B%3CBR%20%2F%3E%20return%20%7B%3CBR%20%2F%3E%20%20headers%3A%20%7B%3CBR%20%2F%3E%20%20%20'Accept'%3A%20'application%2Fjson%3Bodata%3Dverbose'%2C%3CBR%20%2F%3E%20%20%20'Content-Type'%3A%20'application%2Fjson%3Bodata%3Dverbose'%3CBR%20%2F%3E%20%20%7D%3CBR%20%2F%3E%20%7D%3B%3CBR%20%2F%3E%7D%20%20%3CBR%20%2F%3Eprivate%20_getSearchResults()%3A%20Promise%26lt%3BSPResults%26gt%3B%20%7B%0A%20%20%20%20const%20httpOptions%3A%20IHttpClientOptions%20%3D%20this.searchHttpClientOptions%3B%0A%0A%20%20%20%20return%20this.context.httpClient.get(this.context.pageContext.web.absoluteUrl%20%2B%20%60%2F_api%2Fsearch%2Fquery%3Fquerytext%3D'the'%60%2C%20httpOptions)%0A%20%20%20%20.then((response%3A%20Response)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20return%20response.json()%3B%0A%20%20%20%20%7D)%3B%0A%20%20%7D%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-11072%22%20slang%3D%22en-US%22%3ERe%3A%20Use%20SharePoint%20Framework%20and%20Search%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-11072%22%20slang%3D%22en-US%22%3E%3CP%3EHave%20you%20tried%20this%20code%20from%20Chris%20O'Brien%3F%3C%2FP%3E%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fwww.sharepointnutsandbolts.com%2F2016%2F08%2Fasync-rest-calls-promises-httpclient-jquery.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.sharepointnutsandbolts.com%2F2016%2F08%2Fasync-rest-calls-promises-httpclient-jquery.html%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eseems%20that%20you%20need%20some%20kind%20of%20workaround%20when%20calling%20search%20API%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%2F%2F%20workaround%20for%20httpClient%2Fsearch%20API%20issue..%0A%20%20%20%20%20%20headers%3A%20%7B%0A%20%20%20%20%20%20%20%20%22odata-version%22%3A%20%22%22%0A%20%20%20%20%20%20%7D%7D%3C%2FPRE%3E%3CP%3ESeems%20not%20required%20if%20you%20use%20jQuery%20AJAX...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELet%20us%20know%20if%20it%20works.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Microsoft

I am trying to build a search-driven app using the new SharePoint Framework. Whenever I use the built-in HTTP client, I get a 500 error for search. The same GET request runs fine in the same browser session. I've run through SP framework tutorials on GitHub and the list requests work fine (in the same app) but search continues to return a 500 error.

 

Any pointers?

 

Here's a sample of my function trying to call search...

 

protected get searchHttpClientOptions(): IHttpClientOptions {
return {
headers: {
'Accept': 'application/json;odata=verbose',
'Content-Type': 'application/json;odata=verbose'
}
};
}
private _getSearchResults(): Promise<SPResults> { const httpOptions: IHttpClientOptions = this.searchHttpClientOptions; return this.context.httpClient.get(this.context.pageContext.web.absoluteUrl + `/_api/search/query?querytext='the'`, httpOptions) .then((response: Response) => { return response.json(); }); }

 

1 Reply
Highlighted

Have you tried this code from Chris O'Brien?

http://www.sharepointnutsandbolts.com/2016/08/async-rest-calls-promises-httpclient-jquery.html

 

seems that you need some kind of workaround when calling search API

 

// workaround for httpClient/search API issue..
      headers: {
        "odata-version": ""
      }}

Seems not required if you use jQuery AJAX...

 

Let us know if it works.