Friday, May 31, 2019

How to pass and retrieve custom data to a WebResource in Dynamic 365 CRM


Below blog uses deprecated method so I created another one to launch a webresource and pass parameter to it refer here... it uses navigateTo method

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

We all know the concept of querystring parameters in Url. The easiest and visible way to pass the small chunks of data to a webpage.

Here is sample querystring:
https://vjcity.crm4.dynamics.com//WebResources/cm_/html/Dymolabelprinter/vjLabDymo.html?accountId=EEA310A9-8483-E911-A85F-000D3AB0DC71&entitytypename=account


window.open(url) : Not working !!!
Reason: 
  • When we create any new webresource, CRM generates the Url automatically for us without any parameters and CRM know only that registered webresource.
  • When we try to browse the Url using window.open with querystring parameter, it searches the whole string against the registered webresource and throws an 500 error as say webresource not found.

Therefore Dynamic CRM has come up with - Xrm.Utility.openWebResource

Here is an example to understand how to pass custom parameters

function passParameters(accountId)
{
    var webResource = "cm_/html/Dymolabelprinter/NeoLabDymo.html"; // webresource schema name
    var customParameters = encodeURIComponent("entityId=" + accountId + "&entitytypename=account");
    Xrm.Utility.openWebResource(webResource, customParameters);
}


How to retrieve them within webpage


var entityId = getUrlVars()["entityId"];
var entityName = getUrlVars()["entitytypename"];



// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = unescape(window.location.search.replace('?', ''));
    hashes = hashes.replace('Data=', '').replace('data=', '').split(',');
    for (var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

As CRM append querystring data to the Url, it also appends data by default, we might want to get rid of that and just interested in the parameters that we have passed.


Another Limitation
Xrm.Utility.openWebResource always opens in new window in (IE11)
Xrm.Utility.openWebResource() performs different in Chrome as compared to IE11.

openInNewWindow = true/false in parameter window Options is not available for openWebResource()


An unsupported approach described by Andrii here - a33ik.blogspot.co.uk/.../step-by-step-creating-dialog-windows.html


Hope this helps.

Thanks
Vipin Jaiswal
vipinjaiswal12@gmail.com



1 comment:

Priyesh Wagh said...

Nice!
I used the JSON.parse() to pass it on to Xrm.Navigation.OpenWebResource() in v9