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”)
.document.frames[0].document.forms[‘crmForm’];

If you try from CRM 5.0 then use the below code

var XrmPage =document.getElementById(“myIframe”).contentWindow.document.getElementById(“contentIFrame”)
.document.frames[0].Xrm.Page;

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:

    Xrm.Page.ui.controls.get(‘IFRAME_opp’).getObject().contentWindow.document

    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.

    Messaging

    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 == ‘https://way.crm.dynamics.com’)
    {

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

    For ref plese use click this link http://msdn.microsoft.com/en-us/library/cc197015(VS.85).aspx

    Cheers,

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s