Sunday, April 14, 2019

Dynamic CRM Unified Interface Tricks – Retrieve data parameters in web resources

As all of you know Microsoft announce “getQueryStringParameters()” function is deprecated (https://docs.microsoft.com/en-us/dynamics365/get-started/whats-new/customer-engagement/important-changes-cominghttps://docs.microsoft.com/en-us/dynamics365/get-started/whats-new/customer-engagement/important-changes-coming) and they asked to use form context instead of passing parameters. However if you use single web resource with multiple parameters in multiple places (multiple entities or multiple fields in same form) you have to depend it on parameters or have to have mechanism to distinguish execution context. 

We face similar kind of issue, we have single web resource as data inputs for different fields. Each field has different properties and based on that web resource behavior should change. Previously it completely handled by parameters and once “getQueryStringParameters” become obsolete we have to move to some other solution.


We follow following approach and it will reduce our amount of code change.

  • Get the current window URL
    • Using “this.URL” can retrieve current url of the web resource without accessing window object.
  • Set Parameters in form
  • In current situation we defined field name as parameter to distinguish what the field current web resource is referring to


    • Decode url and remove data component from the url
    • Then use regex based function to extract query string (function taken after google search :) ). 
    You can place following code snippet on page on load or inside relevant functionality.

     if (this != null && this != "undefined")
    {
        var decodedUrl = decodeURIComponent(this.URL);
        decodedUrl = decodedUrl.replace("data=",'');
      
        var parameterValue = getQueryString("FieldName", url);  
    }

    function getQueryString ( field, url ) {
        var href = url ? url : window.location.href;
        var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
        var string = reg.exec(href);
        return string ? string[1] : null;
    };