Accessing Xrm.Page through JavaScript MS CRM form in an IFrame

Accessing Xrm.Page or CrmForm through JavaScript from an html page showing CRM form in an IFrame and Saving data inside Iframe

Suppose we have an html page having an iframe that shows a MS CRM Record. If we want to access the attributes on the form or call the save function from our html page(Iframe), we can do it in the following manner.

If you try from CRM 4.0 then use the below code

var crmForm =document.getElementById(“myIframe”).contentWindow.document.getElementById(“contentIFrame”)

If you try from CRM 5.0 then use the below code

var XrmPage =document.getElementById(“myIframe”).contentWindow.document.getElementById(“contentIFrame”)

and if you want to save the Data filled in Html Page(Iframe) follow the below procedure to save the data.

  • First, let’s assume you have an iFrame inside an entity.

    Secondly, we need to uncheck the “Restrict cross-frame scripting” check box on the iFrame properties.

    Lastly, it’s a good idea to make sure our iFrame is using HTTPS to prevent mixed mode security warnings.

    Talking Directly

    If you try to talk directly to the iFrame via something like:


    Unless you are On-Premise, you may encounter the “Access is Denied” warning. This will show itself when dealing with URLs on a different domain (or subdomain). Cross “domain” scripting is a security feature within the browser. Now an easy fix is to disable the browser settings, but this isn’t a good solution.


    Instead of talking directly, we need send a messages to our iFrame. To send a message, we’d do something like:

    Xrm.Page.ui.controls.get(‘IFRAME_opp’).getObject().contentWindow.postMessage(‘test’, “*”)

    To receive the message on the iFrame, you need to listen for the message event. Let’s take a look at the JavaScript on our example iFrame page:

    function receiveMessage(e)

    if (e.origin == ‘’)

    if ( == ‘test’)
    document.getElementById(‘msgBox’).innerHTML = ‘testing…<br />’ + e.origin + ‘<br />’ +;
    if ( == ‘save’)
    saveForm = function()
    document.getElementById(‘msgBox’).innerHTML = ‘saving…’;
    window.attachEvent(“onmessage”, receiveMessage);

    For ref plese use click this link



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s