Skip to content

Tag: Webresource

How to load Javascript from a webresource

Posted in Dynamics 365, Power Platform, and Revive

Imagine an usecase where you can dynamically load a Javascript from the CRM webresources because it doesn’t need to be loaded on every single form load. Perhaps a polyfill to add missing browser functions or a Javascript library like jQuery. Here it is…

function loadJavascriptFromWebresource(strWebresourceName, async)
{
    //sync or async (default)
    if (async == undefined) { var async = true; }

    //build a new webrequest to get the content of the webresource by its name
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/webresourceset?" +
        "$select=content&$filter=name eq '" + strWebresourceName + "'", async);
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function ()
    {
        if (this.readyState === 4)
        {
            req.onreadystatechange = null;
            if (this.status === 200)
            {
                //get the result of the request
                var result = (JSON.parse(this.response)).value[0].content;

                //decode the base64 encoded result
                var script = atob(result);

                //make an indirect eval call to make it globally available
                window.eval(script);
            }
            else
            {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send();
}

My personal usecase was to add a promises polyfill to the Internet Explorer. So, if you combine this post, my post about “Internet Explorer and promises” and you have a promises polyfill in your CRM webresources, you can use promises and do only load the polyfill in case the browser doesn’t support it.

Limits for custom parameters

Posted in Dynamics 365, and Power Platform

Have you ever asked how many characters you can pass as parameter to a Javascript event handler or to a HTML webresource? I do, because I like to give a consultant or the customer the possibility to pass dynamic values in form of JSON objects or even whole functions without touching my code.

HTML webresource

The limit here is at 1.500 characters.

Javascript event handler

I think there no limit. Really. I made a test with up to 2.400.000 characters without any problems. The only thing that I would mention – do not exaggerate it as I did, because the performance suffers quite a lot.

Pass parameters to HTML Webresource

Posted in Dynamics 365, Power Platform, and Revive

A HTML webresource can not just only show HTML elements on your form, furthermore you can work with JavaScript in it and interact with your form. IF you want to access the Xrm namespace of the form inside your HTML webresource, you simply need prepend “window.parent” to your function call.

This looks like in this example:

var myId = window.parent.Xrm.Page.data.entity.getId();

If you need a lot of such calls, you can make your life easier and create your own local variable and assign it the complete Xrm namespace
This could look like this:

var Xrm = window.parent.Xrm;
    var myId = Xrm.Page.data.entity.getId();

Pass parameters to HTML Webresource

Alternatively, you can also pass static values to your webresource. Just add a custom parameter in properties dialog of the webresource.

You can access the custom and contaxt parameters with the following script inside your webresource:

function getWebresourceParameter()
    {
        var userParameters = [], passedParameters = [];
        if (location.search != "")
        {
            var vals = location.search.substr(1).split("&");
            for (var i = 0; i < vals.length; i++)
            {
                vals[i] = vals[i].split("=");
                if (vals[i][0].toLowerCase() == "data" && vals[i][1] != "")
                {
                    var userVals = decodeURIComponent(vals[i][1]).split(",");
                    for (var j = 0; j < userVals.length; j++)
                    {
                        passedParameters[userVals[j].split("=")[0].trim()] = userVals[j].split("=")[1].trim();
                    }
                }
                else
                {
                    passedParameters[vals[i][0]] = vals[i][1];
                }
            }
        }
        return passedParameters;
    }

The custom parameters should be in the form “Name=Value” should be separated by comma.

The debugger shows you all the accessible parameters:

Context parameters are green marked, custom parameters blue.

You can now access your values through the named array.

var wrParameters = getWebresourceParameter();
    var myRelName = wrParameters["relName"];

Have fun with it!