0

I have a custom web part that uses aadHttpClientFactory to authenticate with AAD and then get data from an API secured with AAD.

It works flawlessly most of the time. Periodically, though, it gets the AadHttpClient and then goes to run the get() command to retrieve data from the API but a response never comes back.

It seems to happen most often when the API has been idle for a while - like when I run it for the first time in the morning.

My first thought was that it's an issue with the API not coming back from idle well but if I hit the API from a browser, it works without issue. It seems to only happen from the custom web part which makes me think the get() isn't firing at all.

Here's my code:

console.log("Getting client...");
this.context.aadHttpClientFactory
  .getClient('<API Client ID>')
  .then((client: AadHttpClient): void => {
    console.log("Got client. Calling API...");

    client
      .get('https://www.mydomain.com/myAPI', AadHttpClient.configurations.v1)
      .then((response: HttpClientResponse): Promise<any> => {
        console.log("Got response from API: (" + response.status + ") " + response.statusText);

        if(response.status == 200){
          return response.json();
        }
        else {
          throw "(" + response.status + ") " + response.statusText;
        }

      })
      .then((data: any): void => {
        // process the data
        console.log(data);
      }, (err: any): void => {
        console.log("Error getting API data..." + err);
        this.context.statusRenderer.renderError(this.domElement, "Unable to retrieve data. " + err);
      });
  }, (err: any): void => {
    console.log("Error getting client...");
    this.context.statusRenderer.renderError(this.domElement, err);
  });

As you can see, I have several console.log() statements telling me where I'm at in the code and when I have the problem, I get this in the console:

Getting client...
Got client. Calling API...

And then nothing after that. If the get() command was firing, wouldn't it timeout eventually or return an error code?

If I refresh the page several times, it'll eventually start working again.

Why is my web part hanging?

EDIT: I noticed that I'm getting this error in the console when it fails:

Blocked script execution in '<URL>' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.

  • Here's a thought: Is it possible that an old access token is getting cached on the SharePoint site and I'm only spotting the problem after leaving my browser open long enough for the token to expire? Or should calling getClient() reset all the token info regardless of how long the window has been open? – TheIronCheek Nov 8 at 16:53

Your Answer

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

Browse other questions tagged or ask your own question.