Skip to main content

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

· 12 min read
Jagdish Kumawat
Founder @ Dewiride

A Teams dialog that shows a web page can lead on to a second page, and a third, for steps such as "read this" and then "confirm". Each page submits its step to your agent, and the agent answers with the next page's address. Teams loads it in the same dialog.

Syntax​

On each page, the button that finishes the step:

Syntax (each page)
microsoftTeams.dialog.url.submit({ task: "<step name>" }, "<app ID>");

In the agent, a route per step, answered with the next page:

Syntax (the agent)
TeamsExtension.TaskModules.OnSubmit("<step name>", handler);

// In the handler:
return new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new { title = "<title>", width = <width>, height = <height>, url = "<next page address>" },
},
};
PartWhat it does
{ task: "<step name>" }The page's result, named after the step it finishes
"<app ID>"Your Teams app's ID: the id at the top of its manifest.json
OnSubmit("<step name>", handler)Calls handler when that step's page submits
Continue with urlKeeps the dialog open and loads the next page in it
return null!On the last step: no answer, so Teams closes the dialog

Microsoft's Use dialogs with bots lists the answers a bot can give a task/submit. One of them is ContinueTask, which Allows you to chain sequences of Adaptive Cards together in a wizard or multi-step experience. The docs show it with cards. With a url instead of a card, it worked the same way for pages: Teams loaded the next page in the dialog.

The Example​

The agent for this post welcomes new people to the office. Any message gets a card with a Read the house rules button. The dialog it opens has two pages: rules.html lists three rules and has a Next button, and accept.html asks do you accept the house rules? with an Accept button. After Accept, the dialog closes and the agent thanks the person in the chat.

Prerequisites​

  • An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.
  • The agent serving its wwwroot folder with app.MapStaticAssets(), at a public HTTPS address such as a dev tunnel's.
  • That address's host in your app's validDomains, so Teams loads the pages in a dialog.

Step 1: Write the Two Pages​

The first page, wwwroot/rules.html. Its Next button submits the step named next:

wwwroot/rules.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>House rules</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><strong>Step 1 of 2:</strong> the house rules.</p>
<ul>
<li>Book meeting rooms in Outlook.</li>
<li>Keep the second floor quiet.</li>
<li>Clean up the kitchen after lunch.</li>
</ul>
<button type="button" id="next">Next</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>
// Your Teams app's ID, from its manifest.
const appId = "22222222-2222-2222-2222-222222222222";

async function start() {
await microsoftTeams.app.initialize();
document.getElementById("next").addEventListener("click", next);
}

function next() {
microsoftTeams.dialog.url.submit({ task: "next" }, appId);
}

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

The second page, wwwroot/accept.html. Its Accept button submits the step named accept:

wwwroot/accept.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>House rules</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><strong>Step 2 of 2:</strong> do you accept the house rules?</p>
<button type="button" id="accept">Accept</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>
// Your Teams app's ID, from its manifest.
const appId = "22222222-2222-2222-2222-222222222222";

async function start() {
await microsoftTeams.app.initialize();
document.getElementById("accept").addEventListener("click", accept);
}

function accept() {
microsoftTeams.dialog.url.submit({ task: "accept" }, appId);
}

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

Step 2: Answer Each Step in the Agent​

HouseRulesAgent.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 HouseRules;

[TeamsExtension]
public partial class HouseRulesAgent : AgentApplication
{
private readonly Uri _publicUrl;

public HouseRulesAgent(AgentApplicationOptions options, IConfiguration configuration)
: base(options)
{
// PublicUrl is the agent's public HTTPS address: the dev tunnel's. The pages are in wwwroot.
_publicUrl = new Uri(configuration["PublicUrl"]!);

OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("rules", ShowRulesAsync);
TeamsExtension.TaskModules.OnSubmit("next", ShowAcceptPageAsync);
TeamsExtension.TaskModules.OnSubmit("accept", AcceptAsync);
}

// 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"] = "Welcome to the office! Please read and accept the house rules.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Read the house rules",
["data"] = new JsonObject { ["msteams"] = new JsonObject { ["type"] = "task/fetch" }, ["task"] = "rules" },
},
},
};

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

// task/fetch: step 1, the rules.
private Task<TaskModuleResponse> ShowRulesAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(ShowPage("rules.html"));
}

// task/submit from step 1 ("task": "next"): another continue keeps the dialog open and loads step 2 in it.
private Task<TaskModuleResponse> ShowAcceptPageAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(ShowPage("accept.html"));
}

// task/submit from step 2 ("task": "accept"): confirm in the chat, and no answer closes the dialog.
private async Task<TaskModuleResponse> AcceptAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync("Thanks for accepting the house rules.", cancellationToken: cancellationToken);
return null!;
}

// A continue answer that loads one of the pages in the dialog.
private TaskModuleResponse ShowPage(string page) => new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new { title = "House rules", width = 400, height = 230, url = new Uri(_publicUrl, page).ToString() },
},
};
}

HouseRulesAgent.cs in a macOS-style editor window: the constructor routes the rules dialog with OnFetch and the next and accept steps with OnSubmit, ShowRulesAsync and ShowAcceptPageAsync both return ShowPage with rules.html or accept.html, AcceptAsync sends Thanks for accepting the house rules. and returns null!, and ShowPage builds a continue response with the title House rules, width 400, height 230 and the page's url

  • ShowPage(string page) builds the Continue answer for a page: the same title and size every time, and the page's address, made from the public address and the file name.
  • OnFetch("rules", ShowRulesAsync) opens the dialog with step 1, from the card's button. How to Show a Web Page in a Teams Dialog in the Microsoft 365 Agents SDK in C# covers this first answer and the PublicUrl setting.
  • OnSubmit("next", ShowAcceptPageAsync) answers step 1's Next with step 2's page. This is the move to the next page: a Continue answer to a task/submit.
  • OnSubmit("accept", AcceptAsync) finishes: it thanks the person in the chat and returns null!, so Teams closes the dialog.

Step 3: Go Through the Steps in Teams​

Replace the placeholder app ID on both pages, start the agent with dotnet run, and send it any message in Teams. Select Read the house rules on the card. The dialog opens with step 1:

A Teams dialog from the app Dialog Page Demo, titled House rules: Step 1 of 2: the house rules., a list with Book meeting rooms in Outlook., Keep the second floor quiet. and Clean up the kitchen after lunch., and a Next button

Select Next. The dialog shows step 2:

The same House rules dialog, now showing Step 2 of 2: do you accept the house rules? and an Accept button

Select Accept. The dialog closed, and the agent's message arrived in the chat:

The agent's message in the Teams chat: Thanks for accepting the house rules.

What Each Button Sent​

The agent's log showed three invokes. Their values, in order:

task/fetch value (Read the house rules)
{ "data": { "task": "rules", "type": "task/fetch" }, "context": { "theme": "default" } }
task/submit value (Next)
{ "data": { "task": "next" }, "context": { "theme": "default" } }
task/submit value (Accept)
{ "data": { "task": "accept" }, "context": { "theme": "default" } }

The first two answers were Continue responses, with https://cdzngt35-3980.inc1.devtunnels.ms/rules.html and then https://cdzngt35-3980.inc1.devtunnels.ms/accept.html as the url. The third was {"status":200}, with no body. With ASP.NET Core's request log on (the Microsoft.AspNetCore.Hosting.Diagnostics category at Information), the agent's output showed Teams asking for each page right after the answer that named it:

Agent output (request lines)
Request starting HTTP/1.1 POST http://localhost:3980/api/messages - application/json;+charset=utf-8 1421
Request finished HTTP/1.1 POST http://localhost:3980/api/messages - 200 - application/json;+charset=utf-8 36.4985ms
Request starting HTTP/1.1 GET http://localhost:3980/rules.html - - -
Request finished HTTP/1.1 GET http://localhost:3980/rules.html - 200 1197 text/html 6.3810ms
Request starting HTTP/1.1 POST http://localhost:3980/api/messages - application/json;+charset=utf-8 1265
Request finished HTTP/1.1 POST http://localhost:3980/api/messages - 200 - application/json;+charset=utf-8 14.6511ms
Request starting HTTP/1.1 GET http://localhost:3980/accept.html - - -
Request finished HTTP/1.1 GET http://localhost:3980/accept.html - 200 1075 text/html 0.7379ms
Request starting HTTP/1.1 POST http://localhost:3980/api/messages - application/json;+charset=utf-8 1267
Request finished HTTP/1.1 POST http://localhost:3980/api/messages - 200 0 - 321.3111ms

The last POST answered with no body (200 0), and no page request followed it: the dialog was closed.

Common Mistakes​

MistakeWhat happensDo this instead
Returning null! for a step that isn't the lastTeams closes the dialog instead of showing the next pageAnswer with Continue and the next page's url
The same step name on two pagesBoth buttons reach the same routeGive each page's result its own task name
A next page on a domain that isn't in validDomainsTeams loads pages only from listed domainsList every domain the dialog's pages come from
Leaving the placeholder app ID on one of the pagesThat page doesn't name your appCopy your manifest's id into every page

Troubleshooting​

SymptomLikely causeFix
The dialog closes after NextThe handler for that step returned null!Return ShowPage("<next page>")
Next does nothingThe page's script stopped before the button got its handlerOpen the browser console and read the first error

FAQ​

Why go through the agent instead of linking the pages to each other?​

A link would load the next page without a task/submit, so the agent's routes would never see the step. Through the agent, every step's result reaches your code first, and the agent decides what comes next, as it does for card dialogs.

How do I end the dialog with a message instead of closing it?​

Answer the last step with a Message response: How to Close a Teams Dialog with a Message in the Microsoft 365 Agents SDK in C#.

Is this different from a multi-step card dialog?​

Only in what each step shows. A card dialog answers each step with the next card; this one answers with the next page's url: How to Build a Multi-Step Teams Dialog in the Microsoft 365 Agents SDK in C# shows the card version.

Conclusion​

To show the next web page in a Teams dialog, have each page submit its own step name with microsoftTeams.dialog.url.submit(), route each name with TeamsExtension.TaskModules.OnSubmit, and answer with a Continue response whose url is the next page. On the last step, return null! to close the dialog.

Additional Resources​