Dialog API的messageParent无法与OWA一起使用

时间:2018-03-27 17:03:22

标签: office-js outlook-web-addins

我使用Office Javascript API作为我的Outlook加载项。我正在运行outlook-web-16.01(https://outlook.live.com/owa/)。

我可以通过调用UI.displayDialogAsync来弹出对话框。但是调用UI.messageParent并不会导致DialogMessageReceived在父页面上被触发。我在同一个域上运行父和对话框。

但是,手动关闭对话框会触发父级DialogEventReceived12006

我还注意到加载后该对话框在JS控制台上出现此错误,但不确定它是否相关:

  

无法在'postMessage'上执行'DOMWindow':提供的目标来源('https://outlook.live.com')与收件人窗口的来源('MyOrigin')不匹配。

我正在使用OfficeDev example from GitHub

我的代码是在从here

复制的父页面上启动对话框
function dialogCallback(asyncResult) {
  if (asyncResult.status == "failed") {

      // In addition to general system errors, there are 3 specific errors for 
      // displayDialogAsync that you can handle individually.
      switch (asyncResult.error.code) {
          case 12004:
              console.log("Domain is not trusted");
              break;
          case 12005:
              console.log("HTTPS is required");
              break;
          case 12007:
              console.log("A dialog is already opened.");
              break;
          default:
              console.log(asyncResult.error.message);
              break;
      }
  }
  else {
      dialog = asyncResult.value;
      /*Messages are sent by developers programatically from the dialog using office.context.ui.messageParent(...)*/
      dialog.addEventHandler(Microsoft.Office.WebExtension.EventType.DialogMessageReceived, messageHandler);

      /*Events are sent by the platform in response to user actions or errors. For example, the dialog is closed via the 'x' button*/
      dialog.addEventHandler(Microsoft.Office.WebExtension.EventType.DialogEventReceived, eventHandler);

      console.log(`handler registered`);
  }
}

function messageHandler(arg) {
    console.log(`handler called`);
    dialog.close();
    console.log(arg.message);
}

function eventHandler(arg) {

    // In addition to general system errors, there are 2 specific errors 
    // and one event that you can handle individually.
    switch (arg.error) {
        case 12002:
            console.log("Cannot load URL, no such page or bad URL syntax.");
            break;
        case 12003:
            console.log("HTTPS is required.");
            break;
        case 12006:
            // The dialog was closed, typically because the user the pressed X button.
            console.log("Dialog closed by user");
            break;
        default:
            console.log("Undefined error in dialog window");
            break;
    }
}

  var dialogUrl = 'MyOrigin/outlook/function-file/dialog.html';
  Office.context.ui.displayDialogAsync(dialogUrl, { height: 50, width: 50 }, dialogCallback);

对于HTML对话框,我与this code完全相同。

1 个答案:

答案 0 :(得分:0)

调试Microsoft Office库后,我发现了问题。 为了使消息正常工作,清单文件中的URL应该以https://

为前缀

我更新清单后,一切都开始起作用。

只需确保您在AppDomains中的URL带有https://

前缀
  <AppDomains>
    <AppDomain>https://app.polarcrm.com</AppDomain>
    <AppDomain>https://localhost:44321</AppDomain>
  </AppDomains>

这是我的实施细节: https://yaplex.com/blog/office-addin-messageparent-not-working

enter image description here