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?

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

1 Answer 1

3

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

`&SelectProperties='Title'`

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.