Skip to content

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!

3 Comments

  1. Kalyan

    Thanks Ben. Very useful article. Is there a way of getting other field values on the form and passing them as parameters.

    12.10.2018
    |Reply
    • Hi Kalyan,
      thanks for the feedback.
      You can access them directly over the parent object with “parent.Xrm.Page…”

      14.10.2018
      |Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.