0

I am trying to use Dialog API of Office Add-ins.

According to the doc, we could use Office.context.ui.messageParent to send a message from the Dialog box to the host page (eg, task pane). Whereas, I don't see how we could send a message from the host page to the Dialog box.

Does anyone know how to do this?

SoftTimur
  • 5,630
  • 38
  • 140
  • 292
  • Seems to be a duplicate of https://stackoverflow.com/questions/42572554/how-to-pass-model-to-office-365-dialog-from-word-2016/42585823?noredirect=1#comment72306287_42585823 Please vote up the feature request as indicated in the answer there. – Rick Kirkham May 25 '17 at 18:37

2 Answers2

0

There are 2 possible solutions:

  1. Send data as query params, when you open a pagein dialog box.
  2. If there are in same domain then localstorage should be available in dialog which was opened. setInterval(function () { var value = localStorage.getItem("dataFromDialog"); }, 500) You can write the same value in localStorage in dialog localStorageSetItem("dataFromDialog", "message to parent")
Kingmaker
  • 469
  • 1
  • 6
  • 20
  • Could you explain more for "query params"? For the `localstorage`, how could Dialog inform the host page there is something new in `localstorage`? – SoftTimur May 25 '17 at 15:44
  • When you open a dialog via displayDialog(url), then you can construct url with query parameters like https://localhost?key1=value1&key2=value2 and this values you can read in page what you load in dialog box. Ragarding to localstorage: I think there you can periodically check localStorage from parent whether something changed or not – Kingmaker May 25 '17 at 15:50
  • I did not said that it is convenient. It is just a solution, what I'm using also. If there is any other, then I would be really happy, but I did not find any other solution for that problem. So If you find something it would be good to share it here :) – Kingmaker May 25 '17 at 16:04
  • OK... let's see what others say... It is odd that there is no way to send a message like `post.message`... – SoftTimur May 25 '17 at 16:05
  • @SoftTimur: In your comment a couple hours ago you say "I want Dialog box to send messages to the host page whenever it wants" This can be done with messageParent. But your original question is about the host page sending messages to the Dialog box. There is no API to do this yet. – Rick Kirkham May 25 '17 at 18:39
  • I want the host to send messages to the Dialog box whenever it wants, and the Dialog box listens and reacts to the message. I don't think `query params` or `localStorage` is that convenient... And it is really a pity that there is no API to do so... – SoftTimur May 25 '17 at 18:42
0

This feature is now in preview

See https://learn.microsoft.com/en-us/office/dev/add-ins/develop/parent-to-dialog

Example from the post:

Office.context.ui.addHandlerAsync(
    Office.EventType.DialogParentMessageReceived,
    onMessageFromParent);

function onMessageFromParent(event) {
    var messageFromParent = JSON.parse(event.message);
}
Mafii
  • 7,227
  • 1
  • 35
  • 55