1

I am trying to call search REST apis from an spfx webpart, but I am getting this:

GET http://servername/_api/search/query?querytext=%27*%27

My code looks like this:

let query = `${this.context.pageContext.web.absoluteUrl}/_api/search/query?querytext=%27*%27`

this.context.spHttpClient.get(query, SPHttpClient.configuration.v1)
.then(response: SPHttpClientResponse) => {  console.log(response) })
.catch(error => { console.log(error) })

I am testing the webpart using the workbench under _layouts, and I can verify that the query is valid, and I could paste it in browser and see the results, it just doesn't work from code.

Update:

I had a look at the logs (am using sp 2016 feature pack 2). and I saw this in the logs after I try to load my webpart:

Parent=Request (GET:http://server/_api/search/query?querytext='*') System.NullReferenceException: Object Reference not set to an instance of an object

at

Microsoft.SharePoint.Client.Odata.ODataSerializeUtility.ConvertKeyValueToODataValue(String key, Object value, ProxyContext proxyContext)

any idea?

  • Have you tried with out encode url like ${this.context.pageContext.web.absoluteUrl}/_api/search/query?querytext='*' – Bharat Khunti May 27 '18 at 15:49
  • Hi, yes I did same result. – Brittany Rutherford May 27 '18 at 15:53
  • Does it work in the browser? – Matthew McDermott May 28 '18 at 12:09
  • @MatthewMcDermott yes it works in the browser, but not from code. – Brittany Rutherford May 29 '18 at 16:28
  • can you share the network panel info from your browser? – Arut May 29 '18 at 20:09
3

You need add SelectProperties like as 'Title' or other and it will work from code.

`&SelectProperties='Title'`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.