Wednesday, April 28, 2021

How to open a web resource and pass parameter in Dynamic 365 CRM

 



Dynamic CRM  - Xrm.Navigation.navigateTo

I would be using navigateTo here in this blog to launch a webresource. 


JavaScript which is used to launch webresource and pass parameters.

function launchContactWebResource()

{

debugger;

 

var selectedRow = Xrm.Page.getControl("contactgrid").getGrid().getSelectedRows(); 

var selectedRecordId = selectedRow.getAll()[0].getData().getEntity().getId().replace('{', "").replace('}', "");

var selectedRecordName = selectedRow.getAll()[0].getData().getEntity().getPrimaryAttributeValue();

 

 

var selectedContact =

{

    ContactName: selectedRecordName,

    ContactId: selectedRecordId

};


var pageInput = {

    pageType: "webresource",

    webresourceName: "hel_/html/contact/ShowContacts",  // Web-Resource Schema name

    data: JSON.stringify(selectedContact)   // pass parameter here

};


var navigationOptions = {

    target: 2,

    width: 400, 

    height: 400, 

    position: 1    // Open in Center

};


window.parent.Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(

    function success() {

        // Run code on success         

    },

    function error(error) {

        window.parent.Xrm.Utility.alertDialog(error.message);

    }

);

}



Web-Resource Code to fetch and Display Parameters.


<html>

<head>

<script type="text/javascript">

         

    function onload()

    {

        GetSelectedRecord();               

    }

          

    function GetSelectedRecord()

    {

        debugger;

        var id = "";

        var name = "";

        if (location.search != null) {

            if (location.search.split("=")[1] != null) {

                id = JSON.parse(decodeURIComponent(location.search.split("=")[1]))["ContactId"];

                name = JSON.parse(decodeURIComponent(location.search.split("=")[1]))["ContactName"];

            }

        }

 

        var table = document.getElementById("tblOrgActivity");

        var rowCount = table.rows.length;

        var row = table.insertRow(rowCount);

        row.insertCell(0).innerHTML = id;

        row.insertCell(0).innerHTML = name;

    }      

 

    </script>

</head>

<body onload="baseContacts.onload()">

    <div id="mydata">

        <div>

            <table id="tblOrgActivity" border="1">               

                <tr>

                    <td>Name</td>

                    <td>ID</td>

                </tr>

            </table>

        </div>

 

    </div>

</body>

</html>



Launch Web-Resource in Adjacent TAB


var navigationOptions = {

    target: 2,

    width: { value: 50, unit: "%" },    

    position: 2    // Open in Adjacent Tab    

};






Generic Method to retrieve parameters



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;

}


1 comment:

Anonymous said...

It works well :)