How to Build a Multi-Step Teams Dialog in the Microsoft 365 Agents SDK in C#
Some forms read better in steps, especially when step two depends on step one: pick a workshop, then pick one of that workshop's sessions. A Teams dialog can hold several steps. Each step's button gets the next step's card from your agent, and the card carries what was chosen so far.
Syntax
TeamsExtension.TaskModules.OnSubmit("<step 1 button>", handler); // returns step 2's card, as Continue
TeamsExtension.TaskModules.OnSubmit("<step 2 button>", handler); // returns the next card, or ends the dialog
TeamsExtension.TaskModules.OnSubmit("back", handler); // returns step 1's card, filled in
{
"type": "Action.Submit",
"title": "<button text>",
"data": { "task": "<step name>", "<earlier answer>": "<its value>" }
}
| Part | What it does |
|---|---|
A Continue answer to a submit | Puts the next card in the dialog, with its own title |
The earlier answers in data | Keep step 1's choice once its input is gone: Teams sends a button's data along with the card's inputs |
"associatedInputs": "none" on Back | Sends no inputs, so Back works even when the current step isn't filled in |
| The last step's answer | A Message, or no answer, to end the dialog |
Microsoft's Use dialogs with bots describes it: You can chain Adaptive Cards into a multi-step wizard by returning a ContinueTask response from the submit handler. Each step returns a new card, and the final step returns a MessageTask to close the dialog. In the Agents SDK, those are TaskModuleResponseTypes.Continue and TaskModuleResponseTypes.Message. In my tests, the final message took the card's place in the dialog, which stayed open until the person closed it.
The Example
The agent for this post signs people up for one of two workshops, each with two sessions. Step 1 asks for the workshop, with Next. Step 2 lists that workshop's sessions, with Sign up and Back. Sign up ends with You're signed up for Presentation skills, Friday 11:00.
Prerequisites
- An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.
Step 1: Write the Steps
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.Schema;
using Microsoft.Teams.Apps.TaskModules;
namespace HoSteps;
[TeamsExtension]
public partial class WorkshopAgent : AgentApplication
{
// Each workshop and its sessions.
private static readonly Dictionary<string, string[]> Sessions = new()
{
["Excel basics"] = ["Tuesday 10:00", "Thursday 14:00"],
["Presentation skills"] = ["Wednesday 09:00", "Friday 11:00"],
};
public WorkshopAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("workshop", OpenAsync);
TeamsExtension.TaskModules.OnSubmit("pick-workshop", PickWorkshopAsync);
TeamsExtension.TaskModules.OnSubmit("pick-session", PickSessionAsync);
TeamsExtension.TaskModules.OnSubmit("back", BackAsync);
}
private Task SendCardAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
JsonObject card = Card(
new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "Workshops", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Two workshops this week, two sessions each.", ["wrap"] = true },
},
new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Sign up",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "workshop",
},
},
});
return turnContext.SendActivityAsync(
MessageFactory.Attachment(new Attachment { ContentType = ContentTypes.AdaptiveCard, Content = card }),
cancellationToken);
}
// Step 1 opens with the workshop list.
private Task<TaskModuleResponse> OpenAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(StepOne(workshop: ""));
}
// Step 1's Next: show step 2, with the sessions of the workshop they picked.
private Task<TaskModuleResponse> PickWorkshopAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(StepTwo(request.GetDataString("workshop")));
}
// Step 2's Back: step 1 again, with their workshop still picked.
private Task<TaskModuleResponse> BackAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(StepOne(request.GetDataString("workshop")));
}
// Step 2's Sign up: the last step, so the dialog ends with a message.
private Task<TaskModuleResponse> PickSessionAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
string workshop = request.GetDataString("workshop");
string session = request.GetDataString("session");
TaskModuleResponse done = new()
{
Task = new Response { Type = TaskModuleResponseTypes.Message, Value = $"You're signed up for {workshop}, {session}." },
};
return Task.FromResult(done);
}
private static TaskModuleResponse StepOne(string workshop)
{
JsonObject card = Card(
new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "Step 1 of 2: pick a workshop.", ["wrap"] = true },
Choices("workshop", "Workshop", Sessions.Keys, workshop),
},
new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Next", ["data"] = new JsonObject { ["task"] = "pick-workshop" } },
});
return Show("Sign up for a workshop", card);
}
// Step 2 carries the workshop in its buttons' data: the card has no workshop input any more.
private static TaskModuleResponse StepTwo(string workshop)
{
JsonObject card = Card(
new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = $"Step 2 of 2: pick a session of {workshop}.", ["wrap"] = true },
Choices("session", "Session", Sessions[workshop], ""),
},
new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Sign up", ["data"] = new JsonObject { ["task"] = "pick-session", ["workshop"] = workshop } },
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Back", ["associatedInputs"] = "none", ["data"] = new JsonObject { ["task"] = "back", ["workshop"] = workshop } },
});
return Show($"Sign up for {workshop}", card);
}
private static JsonObject Choices(string id, string label, IEnumerable<string> options, string value)
{
JsonArray choices = new();
foreach (string option in options)
{
choices.Add(new JsonObject { ["title"] = option, ["value"] = option });
}
return new JsonObject
{
["type"] = "Input.ChoiceSet",
["id"] = id,
["label"] = label,
["style"] = "expanded",
["isRequired"] = true,
["errorMessage"] = $"Pick a {label.ToLowerInvariant()}.",
["value"] = value,
["choices"] = choices,
};
}
private static JsonObject Card(JsonArray body, JsonArray actions) => new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = body,
["actions"] = actions,
};
private static TaskModuleResponse Show(string title, JsonObject card) => new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title,
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};
}

StepOne(workshop)builds step 1, with the workshop picked when it's given, through the choice list'svalue.PickWorkshopAsyncanswers Next with step 2 for the workshop that was picked.StepTwo(workshop)builds step 2 from that workshop's sessions. Step 2 has no workshop input, so both its buttons carryworkshopin theirdata.BackAsyncanswers Back with step 1, the workshop still picked.PickSessionAsyncends the dialog with a message, using both answers.
Step 2: Go Through the Steps in Teams
Send the agent any message, select Sign up, and pick a workshop:

Select Next. The dialog's title and card changed to step 2, with that workshop's sessions:

Back brought step 1 back with Presentation skills still selected. Next again, Friday 11:00, and Sign up:

What Each Button Sent
The data of each task/submit, from the agent's log, in order:
{"task": "pick-workshop", "workshop": "Presentation skills"}
{"task": "back", "workshop": "Presentation skills"}
{"task": "pick-workshop", "workshop": "Presentation skills"}
{"task": "pick-session", "workshop": "Presentation skills", "session": "Friday 11:00"}
- Next sent step 1's input,
workshop. - Back sent only its own
data: with"associatedInputs": "none", no inputs. - Sign up sent its
data, with the carriedworkshop, plus step 2's input,session.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Not carrying step 1's answer in step 2's buttons | The last step doesn't know it | Put it in each button's data |
Back without "associatedInputs": "none" | Teams wants step 2's required inputs before going back | Add "associatedInputs": "none" |
One task value for every step's button | One handler has to work out which step it's on | Give each step's button its own task value |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Back shows step 1 empty | Step 1 wasn't given the earlier answer as its inputs' value | Pass it in, and set value |
| The title doesn't change between steps | Each step returned the same title | Give each step's answer its own title |
| The dialog closes after step 1 | Step 1's handler returned null or a message | Return the next step as Continue |
FAQ
Can I keep the answers in state instead of the buttons?
Yes, conversation or user state works too, and it keeps values out of the card. Values in a button's data come back from the client, so check them again on the last step before you use them.
How many steps can a dialog have?
Each step is one more Continue answer, so the code sets the number. Two or three steps read well, and each title can say where the person is.
Conclusion
A multi-step Teams dialog is a chain of Continue answers: each step's button returns the next card, later steps carry earlier answers in their buttons' data, and Back with "associatedInputs": "none" returns the previous card filled in. The last step ends with a message or no answer.
Additional Resources
- Use dialogs with bots, on Microsoft Learn: Multi-step dialog chaining.
- Input.ChoiceSet, on Microsoft Learn:
styleandvalue.
Related
- How to Show an Error in a Teams Dialog in the Microsoft 365 Agents SDK in C#
- How to Close a Teams Dialog with a Message in the Microsoft 365 Agents SDK in C#
- How to Add a Cancel Button That Skips Validation to an Adaptive Card
- How to Prefill the Inputs of an Adaptive Card
- Microsoft 365 Agents SDK Fundamentals in C# — Part 10: Teams Dialogs
