1

Xrm.Page.getControl("webResource1").getObject() is working fine in Sales Dynamics 365, while it's not working in Sales Hub.

The counterpart is to use formContext, but how to call a script in a web resource from another web resource.

Eg. Xrm.Page.getControl("webResource1").getObject().contentWindow.function1() is working fine in Sales, but not in Unified UI.

sudhir
  • 147
  • 1
  • 3
  • 12

2 Answers2

4

Xrm.Page has been deprecated (Deprecated methods are here)

You now have to do the following:

  • When you register your event you must tick the Pass Execution Context as first parameter checkbox
  • In your function you have to add a new parameter called executionContext (the name doesn't really matter)

When you've done the above, you can access the new formContext object which contains most of the methods included in Xrm.Page

More information on formContext here

For example:

function myHandler(executionContext) 
{
  var formContext = executionContext.getFormContext();
  var myWebResource = formContext.getControl("webResource1");
}

Edit to address null getObject error

The object returned from getControl() does have a getObject method (MSDN) which states it will return either:

  • An IFRAME returns the IFrame element from the Document Object Model (DOM).
  • A Silverlight web resource will return the Object element from the DOM that represents the embedded Silverlight plug-in.

If you're getting a null object then you've probably got an error with the name of your webresource.

Open your CRM form in designer mode and and locate your webresource. Double-click to view its properties. You're looking for the field name which in CRM Online is prefixed with "WebResource_"

For example:

enter image description here

jasonscript
  • 6,039
  • 3
  • 28
  • 43
  • Agreed. Tried this one as well, but how do I get the web contents of this web resource? formContext.getControl("webResource1") does not have any getObject function, or it aklways returns null – sudhir Jan 15 '19 at 13:32
  • `getObject` does exist (https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/controls/getobject) so you must have the name of your webresource wrong. See my edit – jasonscript Jan 16 '19 at 01:09
1

There’s another design change causing this null when using formContext.getControl("WebResourceName").getObject() because Sales hub or UCI form tab having web resource is not rendered yet unless it’s clicked/navigated.

Similar topic in Dynamics community thread

GitHub issue