You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/develop/dialog-api-in-office-add-ins.md
+20-11Lines changed: 20 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Use the Office dialog API in your Office Add-ins
3
3
description: Learn the basics of creating a dialog box in an Office Add-in.
4
-
ms.date: 02/12/2025
4
+
ms.date: 02/25/2025
5
5
ms.topic: how-to
6
6
ms.localizationpriority: medium
7
7
---
@@ -12,10 +12,16 @@ Use the [Office dialog API](/javascript/api/office/office.ui) to open dialog box
12
12
13
13
- Sign in a user with a resource such as Google, Facebook, or Microsoft identity. For more information, see [Authenticate with the Office dialog API](auth-with-office-dialog-api.md).
14
14
- Provide more screen space, or even a full screen, for some tasks in your add-in.
15
-
- Host a video that would be too small if confined to a task pane.
15
+
-[Host a video that would be too small if confined to a task pane](dialog-video.md).
16
+
- Show an error, progress, or input screen.
16
17
17
18
> [!TIP]
18
-
> Because overlapping UI elements are discouraged, avoid opening a dialog box from a task pane unless your scenario requires it. When you consider how to use the surface area of a task pane, note that task panes can be tabbed. For an example of a tabbed task pane, see the [Excel Add-in JavaScript SalesTracker](https://github.com/OfficeDev/Excel-Add-in-JavaScript-SalesTracker) sample.
19
+
>
20
+
> - We recommend that you don't use a dialog box to interact with a document. Use a task pane instead. For guidance, see [Task panes in Office Add-ins](../design/task-pane-add-ins.md).
21
+
>
22
+
> - Because overlapping UI elements are discouraged, avoid opening a dialog box from a task pane unless your scenario requires it. When you consider how to use the surface area of a task pane, note that task panes can be tabbed. For an example of a tabbed task pane, see the [Excel Add-in JavaScript SalesTracker](https://github.com/OfficeDev/Excel-Add-in-JavaScript-SalesTracker) sample.
23
+
>
24
+
> To learn more about best practices for implementing a dialog, see [Best practices and rules for the Office dialog API](dialog-best-practices.md).
19
25
20
26
The following image shows an example of a dialog box.
21
27
@@ -32,10 +38,10 @@ The Office JavaScript APIs include a [Dialog](/javascript/api/office/office.dial
32
38
33
39
To open a dialog box, your code, typically a page in a task pane, calls the [displayDialogAsync](/javascript/api/office/office.ui#office-office-ui-displaydialogasync-member(1)) method and passes to it the URL of the resource that you want to open. The page on which this method is called is known as the "host page". For example, if you call this method in script on index.html in a task pane, then index.html is the host page of the dialog box that the method opens.
34
40
35
-
The resource that is opened in the dialog box is usually a page, but it can be a controller method in an MVC application, a route, a web service method, or any other resource. In this article, 'page' or 'website' refers to the resource in the dialog box. The following code is a simple example.
41
+
The resource that is opened in the dialog box is usually a page, but it can be a controller method in an MVC application, a route, a web service method, or any other resource. In this article, "page" or "website" refers to the resource in the dialog box. The following code is a simple example.
- The URL uses the HTTP**S** protocol. This is mandatory for all pages loaded in a dialog box, not just the first page loaded.
@@ -49,7 +55,7 @@ After the first page (or other resource) is loaded, a user can use links or othe
49
55
By default, the dialog box will occupy 80% of the height and width of the device screen, but you can set different percentages by passing a configuration object to the method, as shown in the following example.
For a sample add-in that does this, see [Build Office Add-ins for Excel](https://github.com/OfficeDev/TrainingContent/tree/master/OfficeAddin/02%20Building%20Add-ins%20for%20Microsoft%20Excel). For more samples that use `displayDialogAsync`, see [Code samples](#code-samples).
@@ -58,12 +64,15 @@ Set both values to 100% to get what is effectively a full screen experience. The
58
64
59
65
You can open only one dialog box from a host window. An attempt to open another dialog box generates an error. For example, if a user opens a dialog box from a task pane, they can't open a second dialog box from a different page in the task pane. However, when a dialog box is opened from an [add-in command](../design/add-in-commands.md), the command opens a new (but unseen) HTML file each time it is selected. This creates a new (unseen) host window, so each such window can launch its own dialog box. For more information, see [Errors from displayDialogAsync](dialog-handle-errors-events.md#errors-from-displaydialogasync).
60
66
67
+
> [!NOTE]
68
+
> In Outlook on the web and new Outlook on Windows, don't set the [window.name](https://developer.mozilla.org/docs/Web/API/Window/name) property when configuring a dialog in your add-in. The `window.name` property is used by these Outlook clients to maintain functionality across page redirects.
69
+
61
70
### Take advantage of a performance option in Office on the web
62
71
63
72
The `displayInIframe` property is an additional property in the configuration object that you can pass to `displayDialogAsync`. When this property is set to `true`, and the add-in is running in a document opened in Office on the web, the dialog box will open as a floating iframe rather than an independent window, which makes it open faster. The following is an example.
The default value is `false`, which is the same as omitting the property entirely. If the add-in isn't running in Office on the web, the `displayInIframe` is ignored.
@@ -109,7 +118,7 @@ The host page must be configured to receive the message. You do this by adding a
109
118
110
119
```javascript
111
120
let dialog; // Declare dialog as global for use in later functions.
@@ -146,7 +155,7 @@ function processMessage(arg) {
146
155
The `dialog` object must be the same one that is returned by the call of `displayDialogAsync`. You need to declare the `dialog` object as a global variable. Or you can scope the `dialog` object to the `displayDialogAsync` call with an anonymous callback function as shown in the following example. In the example, `processMessage` doesn't need to close the dialog since the `close` method is called in the anonymous callback function.
The add-in's manifest specifies trusted domains. In the unified manifest for Microsoft 365, this is specified in the "validDomains" property. In the add-in only manifest, this is specified in the **\<AppDomains\>** element.
368
+
The add-in's manifest specifies trusted domains. In the unified manifest for Microsoft 365, this is specified in the "validDomains" property. In the add-in only manifest, this is specified in the **\<AppDomains\>** element.
360
369
361
370
[!include[Unified manifest host application support note](../includes/unified-manifest-support-note.md)]
Copy file name to clipboardExpand all lines: docs/develop/dialog-best-practices.md
+46-26Lines changed: 46 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -1,33 +1,38 @@
1
1
---
2
-
title: Best practices and rules for the Office dialog API
3
-
description: Provides rulesand best practices for the Office dialog API, such as best practices for a single-page application (SPA).
4
-
ms.date: 05/16/2024
2
+
title: Best practices and rules for the Office Dialog API
3
+
description: Provides rules, limitations, and best practices for the Office Dialog API, such as best practices for a single-page application (SPA).
4
+
ms.date: 02/25/2025
5
5
ms.topic: best-practice
6
6
ms.localizationpriority: medium
7
7
---
8
8
9
-
# Best practices and rules for the Office dialog API
9
+
# Best practices and rules for the Office Dialog API
10
10
11
-
This article provides rules, gotchas, and best practices for the Office dialog API, including best practices for designing the UI of a dialog and using the API within a single-page application (SPA).
11
+
This article provides rules, limitations, and best practices for the Office Dialog API, including best practices for designing the UI of a dialog and using the API within a single-page application (SPA).
12
12
13
13
> [!NOTE]
14
-
> To familiarize yourself with the basics of using the Office dialog API, see [Use the Office dialog API in your Office Add-ins](dialog-api-in-office-add-ins.md).
14
+
> To familiarize yourself with the basics of using the Office Dialog API, see [Use the Office Dialog API in your Office Add-ins](dialog-api-in-office-add-ins.md).
15
15
>
16
16
> See also [Handling errors and events with the Office dialog box](dialog-handle-errors-events.md).
17
17
18
-
## Rules and gotchas
18
+
## Rules and limitations
19
19
20
-
-The dialog box can only navigate to HTTPS URLs, not HTTP.
20
+
-A dialog box can only navigate to HTTPS URLs, not HTTP.
21
21
- The URL passed to the [displayDialogAsync](/javascript/api/office/office.ui) method must be in the exact same domain as the add-in itself. It can't be a subdomain. However, the page that is passed to it can redirect to a page in another domain.
22
-
- A host page can have only one dialog box open at a time. The host page could be either a task pane or the [function file](/javascript/api/manifest/functionfile) of a [function command](../design/add-in-commands.md#types-of-add-in-commands).
22
+
- A host page can have only one dialog box open at a time. The host page could be either a task pane or the [function file](/javascript/api/manifest/functionfile) of a [function command](../design/add-in-commands.md#types-of-add-in-commands). Multiple dialogs can be open at the same time from custom ribbon buttons or menu items.
23
23
- Only two Office APIs can be called in the dialog box,
-`Office.context.requirements.isSetSupported` (For more information, see [Specify Office applications and API requirements](specify-office-hosts-and-api-requirements.md).)
26
26
- The [messageParent](/javascript/api/office/office.ui#office-office-ui-messageparent-member(1)) function should usually be called from a page in the exact same domain as the add-in itself, but this isn't mandatory. For more information, see [Cross-domain messaging to the host runtime](dialog-api-in-office-add-ins.md#cross-domain-messaging-to-the-host-runtime).
27
+
- When a dialog box opens, it's centered on the screen on top of the Office application.
28
+
- A dialog box can be moved and resized by the user.
29
+
- A dialog box appears in the order in which it was created.
27
30
28
31
> [!TIP]
29
32
> In Office on the web and [new Outlook on Windows](https://support.microsoft.com/office/656bb8d9-5a60-49b2-a98b-ba7822bc7627), if the domain of your dialog is different from that of your add-in and it enforces the [Cross-Origin-Opener-Policy: same-origin](https://developer.mozilla.org/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy) response header, your add-in will be blocked from accessing messages from the dialog and your users will be shown [error 12006](dialog-handle-errors-events.md#errors-and-events-in-the-dialog-box). To prevent this, you must set the header to `Cross-Origin-Opener-Policy: unsafe-none` or configure your add-in and dialog to be in the same domain.
30
33
34
+
- In Outlook on the web and new Outlook on Windows, don't set the [window.name](https://developer.mozilla.org/docs/Web/API/Window/name) property when configuring a dialog in your add-in. The `window.name` property is used by these Outlook clients to maintain functionality across page redirects.
35
+
31
36
## Best practices
32
37
33
38
### Avoid overusing dialog boxes
@@ -74,29 +79,33 @@ The `close` method doesn't accept a callback parameter, and it doesn't return a
### Best practices for using the Office dialog API in an SPA
145
+
### Best practices for using the Office Dialog API in an SPA
131
146
132
147
If your add-in uses client-side routing, as single-page applications (SPAs) typically do, you have the option to pass the URL of a route to the [displayDialogAsync](/javascript/api/office/office.ui) method instead of the URL of a separate HTML page. *We recommend against doing so for the reasons given below.*
133
148
134
149
> [!NOTE]
135
150
> This article isn't relevant to *server-side* routing, such as in an Express-based web application.
136
151
137
-
#### Problems with SPAs and the Office dialog API
152
+
#### Problems with SPAs and the Office Dialog API
138
153
139
154
The Office dialog box is in a new window with its own instance of the JavaScript engine, and hence it's own complete execution context. If you pass a route, your base page and all its initialization and bootstrapping code run again in this new context, and any variables are set to their initial values in the dialog box. So this technique downloads and launches a second instance of your application in the box window, which partially defeats the purpose of an SPA. In addition, code that changes variables in the dialog box window doesn't change the task pane version of the same variables. Similarly, the dialog box window has its own session storage (the [Window.sessionStorage](https://developer.mozilla.org/docs/Web/API/Window/sessionStorage) property), which isn't accessible from code in the task pane. The dialog box and the host page on which `displayDialogAsync` was called look like two different clients to your server. (For a reminder of what a host page is, see [Open a dialog box from a host page](dialog-api-in-office-add-ins.md#open-a-dialog-box-from-a-host-page).)
140
155
@@ -145,4 +160,9 @@ So, if you passed a route to the `displayDialogAsync` method, you wouldn't reall
145
160
Instead of passing a client-side route to the `displayDialogAsync` method, we recommend that you do one of the following:
146
161
147
162
- If the code that you want to run in the dialog box is sufficiently complex, create two different SPAs explicitly; that is, have two SPAs in different folders of the same domain. One SPA runs in the dialog box and the other in the dialog box's host page where `displayDialogAsync` was called.
148
-
- In most scenarios, only simple logic is needed in the dialog box. In such cases, your project will be greatly simplified by hosting a single HTML page, with embedded or referenced JavaScript, in the domain of your SPA. Pass the URL of the page to the `displayDialogAsync` method. While this means that you are deviating from the literal idea of a single-page app; you don't really have a single instance of an SPA when you are using the Office dialog API.
163
+
- In most scenarios, only simple logic is needed in the dialog box. In such cases, your project will be greatly simplified by hosting a single HTML page, with embedded or referenced JavaScript, in the domain of your SPA. Pass the URL of the page to the `displayDialogAsync` method. While this means that you are deviating from the literal idea of a single-page app; you don't really have a single instance of an SPA when you are using the Office Dialog API.
164
+
165
+
## See also
166
+
167
+
-[Use the Office Dialog API in Office Add-ins](dialog-api-in-office-add-ins.md)
168
+
-[Handle errors and events in the Office dialog box](dialog-handle-errors-events.md)
0 commit comments