When adding a page navigation link, we have the options to set the URL to use to a web resources or an external link. When using either one, we may want to be able to pass a parameter on the URL, but this option is not available from the customization form interface. To address this, I use our old good friend JavaScript to handle the injection of parameters on the URL.
The first step is to add the URL with a query string parameter that contains a key value or token that we can replace. In this example, we are using an external URL with a parameter on the Account entity form. We should add a navigation link with the following information:
We should note that the accid parameter is the token that we are using to replace and inject the actual account id from the form. We should also note the label on the link because it is used to find the actual form element. You may ask why not just use the id, and the answer is that we do not have an option to define our own id which is created by the platform (Dynamics), and we do not know its value until the page is rendered. For the purpose of this article, we are using the label parameter to match the name that was used for the link label.
Once we are getting that one element, we get the onclick property. This is important because the platform uses a JavaScript call (function LoadIsvArea) to enable the navigation instead of the HREF property. Reading the attribute, help us ensure that we are maintaining the same function call that was generated by the platform. The string that is returned by this property contains our target URL.
Our next step is to get the actual account id. This is done with the help of XRM JavaScript framework that is native to the Dynamics CRM Platform. To get any entity main GUID, we just need to call this function within the context of the entity form:
Xrm.Page.data.entity.getId();
For our example, this returns the Account GUID which we then can use to replace accid parameter. The final step is to assign a new onclick handler to the form element with a function that evaluates/executes the contents of our link string. If we add a JavaScript debugger, we will be able to see that our external link now reflects the Account GUID of the current loaded record.
I hope this can show you an approach to dynamically add parameters to a navigation link, and I hope that for the next release Microsoft allows us to add context parameters when adding navigation links without having to use JavaScript or edit the Site Map.
The first step is to add the URL with a query string parameter that contains a key value or token that we can replace. In this example, we are using an external URL with a parameter on the Account entity form. We should add a navigation link with the following information:
- Link Label: My Site
- Link URL: http://www.mysite.com?acccountId=accid
We should note that the accid parameter is the token that we are using to replace and inject the actual account id from the form. We should also note the label on the link because it is used to find the actual form element. You may ask why not just use the id, and the answer is that we do not have an option to define our own id which is created by the platform (Dynamics), and we do not know its value until the page is rendered. For the purpose of this article, we are using the label parameter to match the name that was used for the link label.
function SetNavLink(label){
try{
var navLinkRef = document.querySelectorAll('a[title="View ' + label + '"]');
if (navLinkRef != null) {
var navLink = navLinkRef[0];
var link = navLink.getAttribute('onclick');
var guid = Xrm.Page.data.entity.getId();
link = link.replace('guid', guid);
navLink.onclick = function () {
eval(link);
};
}
}catch(e){}
}
The above function uses the label to get a reference of the control by using the querySelectorAll function. This function returns an array of elements that match that query selector for an anchor tag with a specific title. In our case, we should be using something unique for the label. so the array should contain one element. If this is not the case, we should try to change the label to something more unique.Once we are getting that one element, we get the onclick property. This is important because the platform uses a JavaScript call (function LoadIsvArea) to enable the navigation instead of the HREF property. Reading the attribute, help us ensure that we are maintaining the same function call that was generated by the platform. The string that is returned by this property contains our target URL.
Our next step is to get the actual account id. This is done with the help of XRM JavaScript framework that is native to the Dynamics CRM Platform. To get any entity main GUID, we just need to call this function within the context of the entity form:
Xrm.Page.data.entity.getId();
For our example, this returns the Account GUID which we then can use to replace accid parameter. The final step is to assign a new onclick handler to the form element with a function that evaluates/executes the contents of our link string. If we add a JavaScript debugger, we will be able to see that our external link now reflects the Account GUID of the current loaded record.
I hope this can show you an approach to dynamically add parameters to a navigation link, and I hope that for the next release Microsoft allows us to add context parameters when adding navigation links without having to use JavaScript or edit the Site Map.