title: Accessing From Context From WebResource Hosted In Form Post Xrm.Page Removal
body: <p>What is the best approach to access the FormContext of a form from a WebResource hosted on the form?</p>
  <p>The docs say to <a href="https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/webpage-html-web-resources" rel="nofollow noreferrer">access the Form Context from a html WebResource to use parent.Xrm.Page</a>.</p>
  <blockquote>
  <p>An HTML web resource added to a form can’t use global objects defined by the JavaScript library loaded in the form. An HTML web resource may interact with the Xrm.Page or Xrm.Utility objects within the form by using parent.Xrm.Page or parent.Xrm.Utility, but global objects defined by form scripts won’t be accessible using the parent. You should load any libraries that an HTML web resource needs within the HTML web resource so they’re not dependent on scripts loaded in the form.</p>
  </blockquote>
  <p>But Xrm.Page is deprecated, and will be removed.  When it does, my assumption is parent.Xrm.Page will fail to work at that point in time as well.</p>
  <p>This assumption is echo'd by a closed issue posted at the end of the doc page.</p>
  <blockquote>
  <p>An HTML web resource added to a form can’t use global objects defined by the JavaScript library loaded in
  But if you look at the issues on the page above, someone asks the obvious question:
  To prevent a gap in functionality, how will an HTML web resource included on an entity form access the form's context, when the only supported method available today is being deprecated?
  Can we depend on there being a supported alternative to &quot;parent.Xrm.Page&quot; by the time Xrm.Page is removed?</p>
  </blockquote>
  <p>To which the reply is to use the <a href="https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/controls/getcontentwindow" rel="nofollow noreferrer">getContentWindow</a>, and inject it in from the onload of the form.  This still isn't great because there is no guarantee that the web resource will finish loading in time to be able to accept said value.  A dev even commented as such and posted their work around on the getContentWindow doc page</p>
  <pre><code>function form_onload(executionContext) {
      const formContext = executionContext.getFormContext();
      const wrControl = formContext.getControl(&quot;new_myWebResource.htm&quot;);
      if (wrControl) {
          wrControl.getContentWindow().then(contentWindow =&gt; {
              let numberOfCalls = 0;
              let interval = setInterval(() =&gt; {
                  if (typeof contentWindow.setClientApiContext !== &quot;undefined&quot;) {
                      clearInterval(interval);
                      contentWindow.setClientApiContext(Xrm, formContext);
                  }
                  else
                      //stop interval after 1 minute
                      if (++numberOfCalls &gt; 600) {
                           clearInterval(interval);
                           throw new Error(&quot;Content Window failed to initialize.&quot;);
                      }
              }, 100);
          });
      }
  }
  </code></pre>
  <p>Is this currently the best/recommended approach for getting this to work, or will the parent.Xrm.Page still work even when the Xrm.Page has been removed?</p>