Skip to main content

How to Close a Teams Dialog from Its Web Page with TeamsJS

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A Teams dialog has its own close button, the X in its corner, but a web page inside it can close it too, for example from an OK button. One call to the Teams JavaScript library does it: microsoftTeams.dialog.url.submit(), with no arguments.

How a Page Closes Its Dialog​

A dialog that shows a web page loads it in a frame. The page can't close the frame itself, but it can ask Teams to, through the Teams JavaScript library (TeamsJS). Microsoft's Use dialogs in tabs explains the call: You can call submit() without any parameters if you want Teams to close the dialog, or you can pass an object or string result back to your app as the first parameter, and an appId of the app that opened the dialog as the second parameter.

This post is about the first case: closing the dialog and sending nothing back.

Syntax​

After microsoftTeams.app.initialize(), from code running in the dialog's page:

Syntax
microsoftTeams.dialog.url.submit();
  • microsoftTeams.dialog.url is the part of TeamsJS for dialogs that show a web page.
  • submit() with nothing in the parentheses closes the dialog without a result.

The reference for submit adds: This function is only intended to be called from code running within the dialog. Calling it from outside the dialog will have no effect. It replaced microsoftTeams.tasks.submitTask(), which is deprecated as of 2.8.0.

The Example​

The app Dialog Page Demo is a bot that opens a Teams dialog with a page it serves, notice.html. The page shows a notice about the office and a Got it button that closes the dialog.

Prerequisites​

  • The page's domain in your app's validDomains. Teams loads a page in a dialog only from a domain its manifest lists.

Step 1: Add the Button​

In the page's <body>, below the content, add the button:

wwwroot/notice.html (part)
<p>The office is closed on Friday for maintenance. Please work from home that day.</p>
<button type="button" id="got-it">Got it</button>
  • type="button" makes it a plain button that does nothing until a script handles its click.
  • id="got-it" lets the script find it.

Step 2: Close the Dialog on Click​

Below the TeamsJS script tag, connect to Teams, then handle the button's click:

wwwroot/notice.html (part)
<script>
async function start() {
await microsoftTeams.app.initialize();
document.getElementById("got-it").addEventListener("click", close);
}

// No result: Teams closes the dialog and sends the agent nothing.
function close() {
microsoftTeams.dialog.url.submit();
}

start();
</script>
  • await microsoftTeams.app.initialize() connects the page to Teams first. Only then does the button get its handler, so a click always reaches a connected page.
  • addEventListener("click", close) runs close when someone selects the button.
  • microsoftTeams.dialog.url.submit() asks Teams to close the dialog.

How to Add the Teams JavaScript Library (TeamsJS) to a Web Page from the CDN explains the script tag that loads TeamsJS. The whole page:

A notice.html file in a code editor window: the notice paragraph, a Got it button with the id got-it, the TeamsJS 2.60.0 script tag, and a script whose start function initializes TeamsJS and adds a click handler that calls microsoftTeams.dialog.url.submit() with no arguments.

wwwroot/notice.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Office notice</title>
<style>
body { margin: 0; padding: 0 24px; font-family: "Segoe UI", system-ui, sans-serif; font-size: 14px; }
button { padding: 5px 20px; font: inherit; }
</style>
</head>
<body>
<p>The office is closed on Friday for maintenance. Please work from home that day.</p>
<button type="button" id="got-it">Got it</button>

<script src="https://res.cdn.office.net/teams-js/2.60.0/js/MicrosoftTeams.min.js"
integrity="sha384-oM1pbPWSxyAWgEVI9GzHDR2xzdMMehbAdsl9cH5emcTxX9Qr/kE4IS/Ld5IdcLmy"
crossorigin="anonymous"></script>
<script>
async function start() {
await microsoftTeams.app.initialize();
document.getElementById("got-it").addEventListener("click", close);
}

// No result: Teams closes the dialog and sends the agent nothing.
function close() {
microsoftTeams.dialog.url.submit();
}

start();
</script>
</body>
</html>

Step 3: Select the Button​

Open the dialog in Teams. The page shows the notice and the button:

A Teams dialog from the app Dialog Page Demo, titled Office notice, showing The office is closed on Friday for maintenance. Please work from home that day., and a Got it button.

Select Got it. The dialog closed, and the chat was back as it was.

The bot's request log shows what reached it: the request that opened the dialog, then Teams loading the page, and nothing after the click:

Agent output
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST http://localhost:3980/api/messages - application/json;+charset=utf-8 1422
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 POST http://localhost:3980/api/messages - 200 - application/json;+charset=utf-8 26.4318ms
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 GET http://localhost:3980/notice.html - - -
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 GET http://localhost:3980/notice.html - 200 1034 text/html 8.8725ms

With no result, Teams closes the dialog without telling the app.

Common Mistakes​

MistakeWhat happensDo this instead
Calling submit() from the page that opened the dialogThe reference says it has no effect outside the dialogCall it from the dialog's own page
Using microsoftTeams.tasks.submitTask()It's deprecated as of TeamsJS 2.8.0Use microsoftTeams.dialog.url.submit()
Passing a result without an appIdMicrosoft's docs require an appId with every resultClose with submit(), or send the result with your app's ID

Troubleshooting​

SymptomLikely causeFix
Nothing happens on clickThe script stopped before the button got its handler, for example because TeamsJS didn't loadOpen the browser console and read the first error
The app expected data but got nonesubmit() was called with no argumentsPass the result and your app's ID

FAQ​

Does the app hear about the dialog closing?​

Not from submit() with no arguments. In the example, the bot's log had no request after Got it.

How do I send data back instead?​

Pass the data as the first argument and your app's ID as the second, as Microsoft's docs ask. How to Submit a Web Page Dialog to Your Agent in the Microsoft 365 Agents SDK in C# shows it with a bot built with the Microsoft 365 Agents SDK.

Is this different from the X in the dialog's corner?​

Both close the dialog. The X is Teams' own button in the dialog's corner; a button on your page closes the dialog at the moment your page decides, such as after someone has read a notice.

Conclusion​

To close a Teams dialog from its own page, connect with microsoftTeams.app.initialize() and call microsoftTeams.dialog.url.submit() with no arguments, for example from a button's click handler. Teams closes the dialog and sends your app nothing.

Additional Resources​