Xrm.Page.getControl() not working in Dynamics 365 Sales Hub

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.

2 Answers

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

2

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like