Search code examples
javascriptiframedynamics-crmpostmessagedynamics-crm-2016

postMessage from iFrame no longer works in CRM 2016 on premise


I've been using postMessage to communicate from an iFrame to CRM for some time now. It worked in CRM 2011 and in CRM 2015 but it no longer works in CRM 2016.

iFrame javascript code:

    var pass_data = {
        'refresh': 'false',
        'expand': 'true'
    };        
    window.parent.postMessage(JSON.stringify(pass_data), 'crm url');

CRM javascript:

function setListener() {

  if (window.XMLHttpRequest) {
    //for browsers other than ie
    window.addEventListener("message", receivePostMessage, false);

  } else {
    //ie
    window.attachEvent('onmessage',receivePostMessage);

  }
} 

function receivePostMessage(event) {
   //do something with event.data
   var pass_data = JSON.parse(event.data);
   alert(pass_data);
  }

setListener() gets called on page load and I've confirmed that it is being called. I've tried using a "*" as the target origin and it still doesnt work.

Does anyone know if this is still a viable option in CRM 2016?


Solution

  • CRM objectmodel changed with the introduction of turbo forms(CRM 2016), access the parent object of the form, which would be 2 levels up the iframe.

    function setListener() {
      if (window.XMLHttpRequest) {
        //for browsers other than ie
        window.parent.addEventListener("message", receivePostMessage, false);
      } else {
        //ie
        window.parent.attachEvent('onmessage',receivePostMessage);
      }
    
    window.parent.parent.postMessage(JSON.stringify(pass_data), 'crm url');