Ehtisham Malik
Ehtisham Malik

Reputation: 85

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.

Upvotes: 0

Views: 1414

Answers (2)

Mr. Lee
Mr. Lee

Reputation: 198

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);
        });
    });
}

Upvotes: 5

RBILLC
RBILLC

Reputation: 190

Here is opening an office dialog and registering the callbacks in JS.

async 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(Office.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(Office.EventType.DialogEventReceived, eventHandler);
    }
}

async function messageHandler(arg) {
    console.log('messageHandler: ' + 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;
    }
}

Upvotes: 0

Related Questions