How to pass formContext and XRM to your html webresource
Learn about suported way to pass formContext and XRM to your html webresource
Let’s image that you created a simple HTML web resource that needs to do some work and then pass it back to the form. Previously to do so you would use parent.Xrm. However, the problem is that previous parent.Xrm and Xrm.Page is deprecated. But know you have supported way to do so - welcome getContentWindow.
This function will return you a promise that on success will contain contentWindow property of your iframe that later can be used to pass some data back to iframe. Keep in mind that this instance is read-only, but its properties can be manipulated so you cannot use it to modify the internal content of iframe.
Call syntax will look like this:
Let’s imagine that we have a simple HTML webresource that allow us to search pokemon by name and returns its height. Webresource contains one input for a pokemon name, button to search for pokemon and save button to save height back to the form.
First we need to create function that will be called from the form to set our XRM and formContext objects inside webresource:
Now we need to add function to our form on OnLoad event to pass the value of XRM and formContext. To do so we need to create a webresource with our function and associate script to a form. If you don’t know how to do it check out this tutorial from Microsoft.
The result you can see below.
Hope you will find this article useful.