{knowhow} Getting formContext in Webresource opened by Xrm.Navigate.navigateTo

Ever since Xrm.Navigate.navigateTo has been introduced, it has been a big relief for consultants and customers alike. After all the ability to open a modal dialog was required so much but was missing for quite some time.

But as everyone rushed in to implement the same, issues arose on how to pass data to the webresource from the  parent record. How do I get access to the formContext to get hold of the form values? The main issue is that unlike Xrm.Navigation.openWebResource in which you can pass data using the data parameter in query string. It’s not like that CRM platform does not allow passing of the data to webresource. In the below code, I am passing query string value using the data parameter, similar to the one we used to do using Xrm.Navigation.openWebResource.

var pageInput = {
     pageType: "webresource",
     webresourceName: "cr16e_test.html?data=" + encodeURIComponent("x=a"),

};

var navigationOptions = {
     target: 2,
     width: 500, // value specified in pixel
     height: 400, // value specified in pixel
     position: 1

};

Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(
     function success() {

debugger;
             // Run code on success
     },
     function error() {

debugger;
             // Handle errors
     }

);

The problem is when the webresource loads you cannot get the values using – Xrm.Utility.getGlobalContext().getQueryStringParameters().

While the above function won’t throw an error, the issue remains that you won’t get the data.

 

image

 

The only way to do this is using the javascript – window.location.search

 

image

Due to these problems I have been receiving the perennial question, how do I access formContext inside the webresource that is opened by the API?. Because if we can access the formContext, we can get values of attributes on the form. Since I had one customer requirement as well hanging on this for quite sometime, I decided to finally give this a try and it worked. Sharing this one as I feel this can help lot of consultants and developers alike.

 

I take a very simple example here. I take the contact entity and register the method on load of the form. Below is the code for the same.

var Training = Training || {};




Training.Contact = {
    formContext: null,
    Load: function (e) {
       Training.Contact.formContext = e.getFormContext();
    }

};




parent.getContext = function () {
    return Training.Contact.formContext;

};

 

Load is the function which is called on form load of the contact where we are taking the formContext and storing it inside a property.The important function I have highlighted in yellow. Notice the use of the parent. construct while defining the function. The function is one of the simplest you can have and it just returns the formContext.

All set and done, now time for the acid test. I launch the web resource and on load of the web resource I just call the getContext function using the same parent context.

 

image

Easy wasn’t it? Now since you have the context, you have complete control on the form from within your webresource.

 

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s