Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an HTML file which is a web resource that I added to my entity.

I followed some examples from Microsoft documentation, so that I added to the OnLoad event handler of the form a function onLoad() from a JS file, and the function setClientApiContext(Xrm, formContext) to the script inside HTML but I did not understand where and how I should define Xrm and formContext.

As when I load the page it tells me that "Xrm is not defined", I can't figure out where and how xrm and formContext should be defined.
I figured out I could pass the executionContext to the function and get formContext from there, but I didn't see this on the examples.

What I have tried:

On the HTML:

<pre lang="Javascript"><pre><script>
        
        function onloadHTML() {
            setClientApiContext(Xrm, formContext)
        }

          function setClientApiContext(Xrm, formContext) {
             //….
           }
    </script>





On the OnLoad.js file:
<pre lang="Javascript">function onload(executionContext) {
    var formContext = executionContext.getFormContext();
    var wrControl = formContext.getControl("new_myWebResource.htm");
    if (wrControl) {
        wrControl.getContentWindow().then(
            function (contentWindow) {
                contentWindow.setClientApiContext(Xrm, formContext);
            }
        )
    }
}
Posted
Updated 26-Feb-21 8:01am

1 solution

We spent a good amount of time on a similar problem. Here are my thoughts, if you are not using the form elements or (Form from CRM) you may completely avoid using formContext. This doesn't mean you can't access XRM. You may access XRM by below simple code. It took almost 6 hours for me to find just a line of code.

userInfo = window.parent.Xrm.Utility.getGlobalContext().userSettings;


If you want to still use formContext (assuming you use the form from CRM) you can follow the below-mentioned steps. Remember, you may need to register an event in Dynamics 365 in order to call it on form load.

How to Use FormContext in a Web Resource in Dynamics 365 Power Apps with getContentWindow - Carl de Souza[^]

Thanks
Rushi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900