Skip to main content

How to Show a Web Page in a Teams Dialog in the Microsoft 365 Agents SDK in C#

· 13 min read
Jagdish Kumawat
Founder @ Dewiride

A Teams dialog can show a web page of your own instead of an Adaptive Card. Your agent answers Teams' dialog request with the page's address, and Teams loads the page inside the dialog.

Syntax​

The route and the answer, in the agent:

C#
TeamsExtension.TaskModules.OnFetch("<dialog name>", handler);

// In the handler:
return new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new { title = "<title>", width = <width>, height = <height>, url = "<page address>" },
},
};
PartWhat it does
OnFetch("<dialog name>", handler)Calls handler when someone selects a card button whose data holds "msteams": { "type": "task/fetch" } and "task": "<dialog name>"
TaskModuleResponseTypes.Continue"Show this in the dialog"
titleThe dialog's title, under the app's name
width, heightA number of pixels, or small, medium or large
urlThe address of the page the dialog shows

The answer is the one a card dialog gets, with url where the card would be. Microsoft's Invoke and dismiss dialogs describes url as The URL of the page loaded as an <iframe> inside the dialog. The URL's domain must be in the app's validDomains array in your app manifest. And Use dialogs with bots adds: Each url and fallbackUrl must implement the HTTPS encryption protocol.

The card button that opens the dialog is the same as for a card dialog: How to Open a Teams Dialog from an Adaptive Card in the Microsoft 365 Agents SDK in C# explains it. TeamsExtension needs [TeamsExtension] on the agent class: How to Add the Teams Extension to an Agent in the Microsoft 365 Agents SDK in C# sets it up.

The Example​

The agent for this post shares the office's holiday calendar. Any message gets a card with a Show holiday calendar button, which opens a dialog titled Holiday calendar. The dialog shows holidays.html, a page the agent serves itself: a table of three public holidays.

Prerequisites​

  • An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.
  • A public HTTPS address for the agent, such as a dev tunnel's. Teams loads the page from it.

Step 1: Add the Page​

In the project, create a folder named wwwroot and, in it, holidays.html:

wwwroot/holidays.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Holiday calendar</title>
<style>
body { margin: 0; padding: 0 24px; font-family: "Segoe UI", system-ui, sans-serif; font-size: 14px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 0; border-bottom: 1px solid #d1d1d1; text-align: left; }
</style>
</head>
<body>
<table>
<tr><th>Date</th><th>Holiday</th></tr>
<tr><td>1 January</td><td>New Year's Day</td></tr>
<tr><td>1 May</td><td>Labour Day</td></tr>
<tr><td>25 December</td><td>Christmas Day</td></tr>
</table>

<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>
microsoftTeams.app.initialize();
</script>
</body>
</html>

Step 2: Serve the Page from the Agent​

In Program.cs, add app.MapStaticAssets(); after the agent's endpoints:

Program.cs (end)
app.UseAgents();
app.MapDefaultAgentEndpoints();
app.MapStaticAssets();

app.Run();

MapStaticAssets() serves the files in wwwroot, so the page is at /holidays.html on the agent's address. How to Serve Static Files in ASP.NET Core with MapStaticAssets (C#) explains what it does. It lists the files when the project is built, so restart the agent with dotnet run after adding a page. A page added while the agent runs is still served in Development, with a warning: Fixed: The static asset was not found in the built time manifest in ASP.NET Core.

Step 3: Give the Agent Its Public Address​

The url must be the address Teams uses to reach the page, the public HTTPS one. In appsettings.Development.json, add a PublicUrl setting with your dev tunnel's address:

appsettings.Development.json (excerpt)
{
"PublicUrl": "https://cdzngt35-3980.inc1.devtunnels.ms"
}

Use your own address: it's the one your bot's messaging endpoint starts with. PublicUrl is this example's own setting, not one the SDK reads. The agent reads it in the next step.

Step 4: Answer the Dialog Request with the Page's Address​

HolidayCalendarAgent.cs
using System.Text.Json.Nodes;
using Microsoft.Agents.Builder;
using Microsoft.Agents.Builder.App;
using Microsoft.Agents.Builder.State;
using Microsoft.Agents.Core.Models;
using Microsoft.Agents.Extensions.MSTeams;
using Microsoft.Teams.Apps.TaskModules;

namespace HolidayCalendar;

[TeamsExtension]
public partial class HolidayCalendarAgent : AgentApplication
{
private readonly string _pageUrl;

public HolidayCalendarAgent(AgentApplicationOptions options, IConfiguration configuration)
: base(options)
{
// PublicUrl is the agent's public HTTPS address: the dev tunnel's. The page is wwwroot/holidays.html.
_pageUrl = new Uri(new Uri(configuration["PublicUrl"]!), "holidays.html").ToString();

OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("holidays", OpenPageAsync);
}

// Any message gets a card whose button opens the dialog.
private Task SendCardAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "Next year's public holidays are out.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Show holiday calendar",
["data"] = new JsonObject { ["msteams"] = new JsonObject { ["type"] = "task/fetch" }, ["task"] = "holidays" },
},
},
};

return turnContext.SendActivityAsync(MessageFactory.Attachment(new Attachment { ContentType = ContentTypes.AdaptiveCard, Content = card }), cancellationToken);
}

// task/fetch: the page's address. Teams opens the dialog and loads the page in it.
private Task<TaskModuleResponse> OpenPageAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new { title = "Holiday calendar", width = 400, height = 230, url = _pageUrl },
},
});
}
}

HolidayCalendarAgent.cs in a macOS-style editor window: the constructor builds the page's address from the PublicUrl setting and holidays.html, routes messages to SendCardAsync and the holidays dialog to OpenPageAsync, and OpenPageAsync returns a continue response with the title Holiday calendar, width 400, height 230 and the page's url

  • new Uri(new Uri(configuration["PublicUrl"]!), "holidays.html") joins the public address and the page's name into https://cdzngt35-3980.inc1.devtunnels.ms/holidays.html. The agent builds it once, when it starts.
  • SendCardAsync sends the card. Its button's data holds "msteams": { "type": "task/fetch" } and "task": "holidays".
  • OnFetch("holidays", OpenPageAsync) routes the dialog request for holidays.
  • OpenPageAsync answers with Continue, the dialog's title and size, and url. With url instead of card, Teams shows the page.

Step 5: List the Page's Domain in the Manifest​

Teams loads a page in a dialog only from a domain in the validDomains list of your app's manifest. Open your app package's manifest.json and look for it. If the page's host isn't there, add it and raise the version:

manifest.json (excerpt)
"version": "1.0.1",
"validDomains": [
"cdzngt35-3980.inc1.devtunnels.ms"
]

Then upload the new package to Teams. How to Add a Valid Domain to a Microsoft Teams App Manifest shows each step, from editing the file to the upload.

Step 6: Open the Dialog in Teams​

Start the agent with dotnet run, and send it any message in Teams. The card arrives:

The agent's card in Teams: Next year's public holidays are out., and a Show holiday calendar button

Select Show holiday calendar. The dialog opens with the page in it:

A Teams dialog with the app icon, Dialog Page Demo and the title Holiday calendar at the top, an X to close it, and the page's table: Date and Holiday, 1 January New Year's Day, 1 May Labour Day, 25 December Christmas Day

Teams drew the dialog's frame: the app's icon and name, the title and the X. Everything under the title is the page.

note

If the dialog shows a Microsoft Dev Tunnels page that starts with You are about to connect to a developer tunnel instead of your page, that's the tunnel's warning for browsers: Fixed: "You Are About to Connect to a Developer Tunnel" in a Teams Dialog.

What Teams Sent and What the Agent Answered​

The agent's log showed the invoke. Its value:

task/fetch value
{
"data": {
"task": "holidays",
"type": "task/fetch"
},
"context": {
"theme": "default"
}
}

The SDK matched "task": "holidays" and sent the handler's answer back as the invoke's response:

Invoke response
{
"status": 200,
"body": {
"task": {
"type": "continue",
"value": {
"title": "Holiday calendar",
"width": 400,
"height": 230,
"url": "https://cdzngt35-3980.inc1.devtunnels.ms/holidays.html"
}
}
}
}

Then Teams asked for the page. With ASP.NET Core's request log on (the Microsoft.AspNetCore.Hosting.Diagnostics category at Information), the agent's output showed the invoke, then the page's request coming in through the tunnel:

Agent output
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 POST http://localhost:3980/api/messages - application/json;+charset=utf-8 1424
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 POST http://localhost:3980/api/messages - 200 - application/json;+charset=utf-8 37.7970ms
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/1.1 GET http://localhost:3980/holidays.html - - -
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 GET http://localhost:3980/holidays.html - 200 932 text/html 11.1161ms

The agent answered twice: the dialog's details through /api/messages, and the page itself, 932 bytes of HTML, from wwwroot.

Common Mistakes​

MistakeWhat happensDo this instead
The agent's local address, http://localhost:3980, as urlMicrosoft's docs require HTTPS, and localhost is each viewer's own computerUse the public HTTPS address, such as the dev tunnel's
The page's host missing from validDomainsThe dialog stays empty, and Teams on the web shows a bannerAdd the host and upload the package again

Troubleshooting​

SymptomLikely causeFix
Empty dialog, and the banner If you're having issues with this app, try the desktop versionThe page's host isn't in validDomains, or the page never called app.initialize()Fixed: "If You're Having Issues with This App, Try the Desktop Version" in a Teams Dialog
The dialog shows a dev tunnel warningThe tunnel's warning page for browsersFixed: "You Are About to Connect to a Developer Tunnel" in a Teams Dialog
A grey sad-page icon, and refused to connectThe page sends a Content Security Policy whose frame-ancestors doesn't include Teams on the webFixed: Framing '…' Violates the Following Content Security Policy Directive: "frame-ancestors …" in a Teams Dialog
Unable to reach app. Please try again.The agent or its tunnel isn't running, or no route matched the task valueStart them, and check the agent's log for the invoke
A warning that the static asset was not found in the built time manifestThe page was added while the agent ranRestart the agent: Fixed: The static asset was not found in the built time manifest in ASP.NET Core

FAQ​

Can the page send something back to the agent?​

Yes. The page calls microsoftTeams.dialog.url.submit() with a result and your app's ID, and the agent gets it as a task/submit invoke: How to Submit a Web Page Dialog to Your Agent in the Microsoft 365 Agents SDK in C#.

Does the page have to come from the agent?​

No. url can point to any HTTPS page whose domain is in validDomains. Serving it from the agent keeps one address to manage, and the dev tunnel already makes it public.

What is fallbackUrl?​

An optional address next to url. Microsoft's Create and send dialogs describes it: If a client does not support the dialog feature, this URL is opened in a browser tab.

Conclusion​

To show a web page in a Teams dialog, serve the page from the agent with app.MapStaticAssets(), answer the task/fetch invoke with a Continue response whose url is the page's public HTTPS address, and list the page's host in the manifest's validDomains. The page loads TeamsJS and calls app.initialize().

Additional Resources​