Refreshing a Web Resource in Unified Interface with JavaScript (The New Way)

4 Comments

This post comes from a great find courtesy of an elite dev I work with who goes by “PV”. We had a requirement to reload / refresh an HTML web resource on a page when a user clicked into a tab to view it. This used to be reasonably straight forward in previous D365 versions and JavaScript – get the Web Resource using getSrc, set the Web Resource to null and then back to itself using setSrc. However, this wasn’t working for us in the latest version of D365 / Power Apps.

Here’s how the new code works.

First, get the name of the tab you’re working with. I have a web resource on the Summary tab of the Account, SUMMARY_TAB:

The Web Resource simply displays the current date and time, so we can see if the date/time is updated when I click out of the tab and back into it:

The name of the web resource is WebResource_HelloWorld:

Here’s the code. In summary:

  1. Get the control using getControl
  2. Get the source using getSrc
  3. setSrc to about:blank
  4. setSrc back to the real source using a setTimeout
function OnLoad(executionContext) {
   var formContext = executionContext.getFormContext();

   var SummaryTab = formContext.ui.tabs.get("SUMMARY_TAB");
   SummaryTab.addTabStateChange(TabClicked);
}

function TabClicked(executionContext) {
   var formContext = executionContext.getFormContext();

   var webResource =formContext.getControl("WebResource_HelloWorld");
   var src = webResource.getSrc();

   var aboutBlank = "about:blank";
   webResource.setSrc(aboutBlank);

   setTimeout(function(){ 
      webResource.setSrc(src);
   }, 1000);
}

Now, rerunning the code, tabbing to a different tab and back to the Summary will reload the web resource and update the date/time:

And after tabbing:

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

4 Responses to Refreshing a Web Resource in Unified Interface with JavaScript (The New Way)

  1. Hey Carl,

    That was enormously helpful. I turned through tonnes of stuff online, but your clearly documented material got me as close as I needed to resolve my CRM 365 UX Web resource refresh. Superb!

    Thanks

    Chrisogonas

  2. Hi Carl,

    Same method is not working when I try in onsave method. Web resource is not loading after save. Could you please let me know what needs to be modified? Thanks in advance.

    Thanks,
    Anil.

  3. Hi Carl,
    I’ve followed you for a long time and your solution saved me a lot of time too.

    Back to this article: for me, on an on-premise installation, it worked but there is short message but bold “resource not found” which appears. I have reduced the delay in the timer but still there is a brief flicker which created an unpleasant user experience.
    So in the end I have duplicated my resource, delete everything un-necessary and empty the paragraph of any text. Basically simulating my ressource but empty. I used that instead of “about:blank”. Now the flicker is not noticeable.
    I am posting this just in case somebody else runs into the same situation.
    Thanks again for the article

Leave a Reply

Your email address will not be published. Required fields are marked *