2

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.

4
  • 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? Nov 8, 2019 at 16:53
  • Do you happen to have resolved this issue? I get the same problem. Any help would be appreciated. :) Aug 28, 2020 at 14:25
  • I created a bug report with Microsoft which they confirmed was a problem on their side with how custom web parts share access tokens with out of the box web parts. Still waiting on confirmation that it’s fixed. Aug 29, 2020 at 16:53
  • If you have any out of the box web parts on the same page, remove them or move them to different pages. If your custom web part starts working, you have the same problem I have. Aug 29, 2020 at 16:54

1 Answer 1

0

I created a bug report with Microsoft shortly after creating this question and quickly found that several other people were having the same issue. Microsoft confirmed this to be a bug with how web parts share access tokens.

My ticket got combined with another slightly different but similar issue related to the same token sharing functionality and while I think they've addressed my specific issue, as of this answer's creation (March 30, 2021), Microsoft isn't willing to confirm that they've fixed anything and the second issue that was combined with mine definitely hasn't been addressed. Your mileage may vary.

You can track the ticket's progress over on GitHub.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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