Sharepoint Online SPFx Search Rest Api 403

In case you develop an SPFx and you try to use RestApi for Search (or maybe also with the SP Rest) and you get 403, this in because your Webpart is running in his own scope, under an IFRAME.


The solution is to dynamically take your Absolute Url
    let filesQuery = this.context.pageContext.web.absoluteUrl;

Every SPFx Webpart has his own SPPageContextInfo.

P.S.
In case you choose to user SPHttpClient, you need 2 things to get over your 500 error:
1. use Default configuration (that should be v3 or v4)
2. change Accept header odata part to verbose

    this.context.spHttpClient.get(filesQuerynew SPHttpClientConfiguration({}),
      { headers: { Accept: "application/json;odata=verbose;charset=utf-8" } })  
      .then((responseSPHttpClientResponse=> {  
        response.json().then((responseJSONany=> {  
          console.log(responseJSON); 
          //your code ...
        });  
      }); 

Comments

Popular posts from this blog

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

MatDatePicker Errors