Accessing HTML controls inside iFrame

dynamics-crm-2011

Solution

Here's an example how you copy a value from a CRM field to a control in an embedded HTML control in an IFRAME. I'm assuming the names of the web resource and the field. You'll have to adapt those. You also might throw in a try-catch in case CRM throws in en exception (got the joke?) and please mind that I'm typing the code on my phone so there might be a typo somewhere (auto-correction, yey).

var source = Xrm.Page.data.entity.attributes.get("oneCoolField")
var information = source.getValue();

var customHtml = Xrm.Page.ui.controls.get("WebResource_EmbeddedHtmlContent");
var destination = customHtml.getObject().contentWindow.document;
if(destination) {
  var customControl = destination.getElementById("elementToAccess");
  if(customControl) {
    customControl.value = information;
  }
}

EDIT:

This gets you to the web resource.

var customHtml = Xrm.Page.ui.controls.get("WebResource_EmbeddedHtmlContent");

This gets you to the DOM of the IFRAME.

var destination = customHtml.getObject().contentWindow.document;

This gets you to the control on the custom page.

var customControl = destination.getElementById("elementToAccess");

This gets you the contents of the control.

var contents = customControl.innerHTML;

Which part fails on your computer?

Problem

How do you access html controls inside an iframe from javascript in CRM? I have: ``` var height = document.getElementById("IFRAME_TransactionProduct_RA").contentWindow.document.getElementById("txt").value; ``` but that results in "Error on page" and the content is not loaded. The element I want to access is an html input with id of 'txt': ``` <input id="txt" type="hidden" /> ```

Original source