0

I have created an Outlook Add-in, I am is using SSO to authenticate users, I can not get messageParent to work. Below is what i am doing any help would be greatly appreciated

Page: Weclome.html On Weclome.html on Button click I am opening a dialog box using displayDialogAsync.

 $("#btnLoginPopup").click(function () {
                //Set up dialog redirct and callback method
                Office.context.ui.displayDialogAsync("https://someUrl.co.uk/Login/Login.html", { height: 50, width: 50},
                    function (asyncResult) {
                        if (asyncResult.status == Office.AsyncResultStatus.Failed) {
                            console.log("Could not open login page", event);
                        }
                        else {
                            console.log("Successfully opened");
                            dialog = asyncResult.value;
                            dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
                        }
                     
                    });
            });

On the same page I have this callback function

function processMessage(arg) {
    //close dialog
    dialog.close();
    console.error("Back");
}

Login.html As soon as this page loads the User is redirected to a external Login page (on a different domain) then they are redirected to a second page LoginCallback.html (on the original domain) after the have been authenticated. When this page load there is a button when clicked should be sending a message back to the parent page but is not working. If I do not have the URL of the external Login in the AppDomains manifest file there is no error but the processMessage function is ever called. If i do have the external url in AppDomains then I get this error

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://SomeUrlAuthUrl.co.uk') does not match the recipient window's origin ('https://SomeUrl.co.uk').

 $("#btnLogin").click(function () {
                Office.context.ui.messageParent("In", { targetOrigin: "https://someUrl.co.uk/" });
            });

Manifest

<AppDomains>
<AppDomain>https://SomeUrl.co.uk</AppDomain>
<AppDomain>https://SomeUrlAuthUrl.co.uk</AppDomain>
</AppDomains>

Thanks

I have tired removing and adding the external url from AppDomains and using

Office.context.ui.messageParent("In", { targetOrigin: "*" }) 

and

Office.context.ui.messageParent("In")
Ritesh734
  • 5
  • 2

2 Answers2

0

What you are seeing is expected. The error is true. Your Welcome.html task pane, which is the parent page, is at SomeUrl.co.uk. Your dialog, which is the child page, is at SomeUrlAuthUrl.co.uk. When your child page calls messageParent, it is sending a message to the parent page, at SomeUrl.co.uk, so the value of targetOrigin should be SomeUrl.co.uk, not SomeUrlAuthUrl.co.uk. For more information, see Cross-domain messaging to the host runtime.

Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32
  • Hi thank you for your response, sorry if I wasn't clear, but the Welcome.html, Login.html and LoginCallback.html are on the same domain. There is a button on Welcome.html which when clicked opens Login.html in a dialog window. We then redirect the user to an external domain then once authenticated we return to the original domain to the page LoginCallback.html. Then once back on the original domain we call messageParent. – Ritesh734 Mar 20 '23 at 21:11
0

I have got Office.context.ui.messageParent working, at the top of my js script I had the below code. For some reason after removing this it started working.

window.addEventListener = ('error', function (ev) {
console.error("Got Error");
console.error(ev);
console.error(ev.type);
});

Thanks for you help

Ritesh734
  • 5
  • 2