Refreshing a HTML Web Resource embedded on Form via JavaScript

Photo by Niels Kehl on Unsplash

Refreshing a HTML Web Resource embedded on Form via JavaScript

When you want to refresh a HTML Page embedded on a Form in MS Dynamics 365 CRM on any event or a button click, the below code might come handy to you.

The following pointers explain working of the code:

  • Obtaining the control of Web Resource on form.

  • Extracting the source of Web Resource in control using Client API reference getSrc().

  • Setting the source in control using Client API reference setSrc(string) initially to null.

  • Getting the client URL of the CRM.

  • Then, in the extracted source, we are checking whether it contains //webresources or /webresources. And depending upon it, we are forming a new source string with the help of Client URL and /webresource/prefix_yourwebresourcename, the latter is obtained dynamically from the extracted source in the below code.

  • If extracted source contains /webresources, then we are appending an extra "/" between Client URL and /webresource/prefix_yourwebresourcename, whereas if it contains //webresources then we are directly using it. This is just because to have some difference in source string for Web Resource every time the below piece of code executes, otherwise you won't see any change in HTML refresh even though the code will execute without any error.

  • clientURL/webresource/prefix_yourwebresourcename and clientURL//webresource/prefix_yourwebresourcename, both will point towards the same HTML.

Note:

  • For any form based event, make sure to pass the execution context as the first parameter.

  • For refreshing the HTML on ribbon button click, do remember to pass the primaryControl.

Code:

function RefreshCustomMapWebResource(objContext){
    var objFormContext = (objContext != null) ? objContext.getFormContext() : null;
    // For refreshing on click of Ribbon Button, use the below commented line of code and comment the above line
    //var objFormContext = (objContext != null) ? objContext : null;
    var sLatitudeField = "address1_latitude";
    var sLongitudeField = "address1_longitude";
    try{
        if((objFormContext != null) && objFormContext.data.entity.getIsDirty() &&
        (objFormContext.getAttribute(sLatitudeField).getIsDirty() || objFormContext.getAttribute(sLongitudeField).getIsDirty()))
        {
            var sWebResourceName = "WebResource_CustomMap";
            var objWebResourceControl = objFormContext.getControl(sWebResourceName);
            if(objWebResourceControl != null)
            {
                var objSrc = objWebResourceControl.getSrc();
                if(objSrc != null)
                {
                    objWebResourceControl.setSrc(null);
                    var sURL = Xrm.Utility.getGlobalContext().getClientUrl();
                    if(objSrc.toLowerCase().indexOf("//webresources") > -1)
                    {
                        objSrc = sURL + objSrc.toLowerCase().substring(objSrc.indexOf("/webresources"));
                    }
                    else if (objSrc.toLowerCase().indexOf("/webresources") > -1)
                    {
                        objSrc = sURL + "/" + objSrc.toLowerCase().substring(objSrc.indexOf("/webresources"));
                    }
                    objWebResourceControl.setSrc(objSrc);
                }
            }
        }
    }
    catch (ex) {
        alert("CustomMapWebResource :: " + ex.message);
    }
}

Happy CRM-ing! Ciao.