outlookoffice-jsoutlook-web-addins

Logic of dialog closed in office javascript API. (Outlook add-in)


How to check if dialog box is closed in outlook add-in?

I want to clear localstorage of browser once I close the dialog box.

I am using Office.context.ui.displayDialogAsync for dialogs.


Solution

  • When you create the dialog, you need to get the dialog handle, and add a dialog closed event listener:

    export async function displayDialogAsync (dialogUrl: string): Promise<void> {
        return new Promise<void>(resolve => {
            
            const dialogClosed = async (_: any): Promise<void> => {
                // do whatever you need when the dialog is closed  
                resolve();
            };
        
            Office.context.ui.displayDialogAsync(url, dialogOptions, (result: Office.AsyncResult) => {
                dialog = result.value;
                dialog.addEventHandler(Office.EventType.DialogEventReceived, dialogClosed);
            });
        });
    }