control.getContentWindow() method in Dynamics 365/ PowerApps can help you get formContext in your webresource page. And in-fact a wide range of possibilities. Have you explored it yet?

Recently I bumped into this method and it unraveled before me a wide range of possibilities. And one of them is the perennial problem to access formContext in webresources embedded inside your form.

So let’s see how you can do this.

First things first – getContentWindow() method only works on webresources and iframes. In this example I am going to use a HTML webresource embedded on the form.

On the contact form I am embedding a webresource with the below content.

image

Quite basic but observe for the method – setFormContext

We will come to this method in a moment. As a next step, on the form load of the contact entity I register the below function.

function contactFormLoad(e) {
    var wrName = ‘WebResource_embedded’;
    var formContext = e.getFormContext();
    var wrControl = formContext.getControl(wrName);
    if (wrControl) {
      
wrControl.getContentWindow().then(
          function (contentWindow) {
             contentWindow.setFormContext(formContext, Xrm);
          }

       );
    }
}

The important part I have already highlighted in yellow. As you can see I am using the getContentWindow() method to access the WebResource and then invoke it’s setFormContext method. And what more I am passing both the Xrm context as well as the formContext.

Cool isn’t it. What an easy way to access the contentDocument methods. Similarly you can access other webresource page methods from the entity form. Now when I load the form, the code executes and set’s the formContext in the HTML webresource.

To test this, I use developers tool and navigate to the webresource frame.

image

And it just works fine. Cool isn’t it?

Hope this helps! Follow my blog to learn more cool stuffs about Dynamics 365 and PowerApps.

Debajit Dutta

(Dynamics MVP)

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

Our product offerings:

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

Notes Manager (https://debajmecrm.com/add-metadata-to-your-notes-and-attachments-in-dynamics-notes-metadata-manager-from-xrmforyou-com/)

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

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

2 thoughts on “control.getContentWindow() method in Dynamics 365/ PowerApps can help you get formContext in your webresource page. And in-fact a wide range of possibilities. Have you explored it yet?”

    1. Hi Nataraj,
      Thanks for your comments and reading my blog. Does the name of the function really matter. It can be anything I believe. The critical part is getting the contentWindow which is basically the webresource window. Once you get it, you can access any method declared within the webresource. The name I guess really doesn’t matter.
      Please correct me if I am wrong!
      Cheers!
      Debajit

Comments are closed.