How to Open a Teams Dialog from an Adaptive Card in the Microsoft 365 Agents SDK in C#
A Teams dialog is a window that opens over the chat, with its own Adaptive Card. Your agent opens one from a card button: Teams asks the agent what the dialog should show, and the agent answers with a title, a size and a card.
Syntax
The button, in the card the agent sends to the chat:
{
"type": "Action.Submit",
"title": "<button text>",
"data": {
"msteams": { "type": "task/fetch" },
"task": "<dialog name>"
}
}
The route and the answer, in the agent:
TeamsExtension.TaskModules.OnFetch("<dialog name>", handler);
// In the handler:
return new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new { title = "<title>", width = "<size>", height = "<size>", card = <the dialog's card> },
},
};
| Part | What it does |
|---|---|
"msteams": { "type": "task/fetch" } | Tells Teams this button opens a dialog, instead of sending a message |
"task": "<dialog name>" | The name your agent routes on |
OnFetch("<dialog name>", handler) | Calls handler when Teams asks for that dialog. The SDK reads the task value from the button's data |
TaskModuleResponseTypes.Continue | "Show this in the dialog" |
title | The dialog's title, under the app's name |
width, height | small, medium or large, or a number of pixels |
card | The Adaptive Card the dialog shows, as a TeamsAttachment |
Microsoft's Use dialogs with bots explains the button: The data object in Action.Submit contains a msteams object so it doesn't interfere with other properties in the card. When someone selects it, Teams sends the agent an invoke named task/fetch, and the answer decides what opens.
TeamsExtension is the Teams extension's property, which 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 is a front desk. Any message gets a card with a Show opening hours button, which opens a dialog titled Opening hours with the desk's hours. The dialog has no buttons of its own: you close it with its X.
Prerequisites
- An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.
Step 1: Send the Card and Answer the Dialog Request
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 HoOpen;
[TeamsExtension]
public partial class FrontDeskAgent : AgentApplication
{
public FrontDeskAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("hours", OpenHoursAsync);
}
// A card with a button that opens a 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"] = "Front desk", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Visitors, parcels and parking: the front desk helps with all three.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Show opening hours",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "hours",
},
},
},
};
return turnContext.SendActivityAsync(
MessageFactory.Attachment(new Attachment { ContentType = ContentTypes.AdaptiveCard, Content = card }),
cancellationToken);
}
// Teams asks what to show in the dialog: a card with the opening hours.
private Task<TaskModuleResponse> OpenHoursAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "Monday to Thursday: 8:00 to 18:00", ["wrap"] = true },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Friday: 8:00 to 16:00", ["wrap"] = true },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Saturday and Sunday: closed", ["wrap"] = true },
},
};
TaskModuleResponse response = new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title = "Opening hours",
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};
return Task.FromResult(response);
}
}

SendCardAsyncsends the card with the button. Its data carries"task": "hours".OnFetch("hours", OpenHoursAsync)routes the dialog request forhours.OpenHoursAsyncanswers withContinue: the dialog's title, its size, and its card, wrapped in aTeamsAttachmentwith the Adaptive Card content type.
Step 2: Open the Dialog in Teams
Send the agent any message. The card arrives:

Select Show opening hours:

Teams put the app's name and icon above the title, and an X to close it. Closing it sends the agent nothing.
What Teams Sent and What the Agent Answered
The agent's log showed the invoke. Its value:
{
"data": {
"task": "hours",
"type": "task/fetch"
},
"context": {
"theme": "default"
}
}
The button's data arrived under data, with the msteams object turned into "type": "task/fetch", and context.theme says which Teams theme the person uses. The SDK matched "task": "hours" and sent the handler's answer back as the invoke's response:
{
"status": 200,
"body": {
"task": {
"type": "continue",
"value": {
"title": "Opening hours",
"width": "medium",
"height": "small",
"card": {
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"type": "AdaptiveCard",
"version": "1.5",
"body": [
{
"type": "TextBlock",
"text": "Monday to Thursday: 8:00 to 18:00",
"wrap": true
},
{
"type": "TextBlock",
"text": "Friday: 8:00 to 16:00",
"wrap": true
},
{
"type": "TextBlock",
"text": "Saturday and Sunday: closed",
"wrap": true
}
]
}
}
}
}
}
}
When No Route Matches
A typo in the task name is easy to make. With the route registered as opening-hours and the button still sending hours, selecting the button showed this:

The agent's log showed the invoke arriving, and no answer from a handler. When no route answers an invoke, the SDK responds with HTTP 501, Not Implemented, and Teams shows Unable to reach app. Please try again., the same message as when the agent isn't running at all. So when you see it, check the agent's log before the network: if the invoke is there, compare its task value with your routes.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
No msteams object in the button's data | The button sends the agent a message with the card's values, and no dialog opens | Add "msteams": { "type": "task/fetch" } |
A different task value in the card and in OnFetch | Unable to reach app. Please try again. | Use one name in both places |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Unable to reach app. Please try again. with the invoke in the log | No route matched the task value | Fix the name, or the OnFetch route |
| Unable to reach app. Please try again. with nothing in the log | The agent or its tunnel isn't running | Start them, and check the bot's endpoint |
CS0103 for Teams | The extension's property is called TeamsExtension | Write TeamsExtension.TaskModules |
FAQ
Can a dialog open without a card button?
From a bot, Microsoft's guide names two ways: a card button with task/fetch, as here, and a deep link to a dialog. Message extensions open dialogs too.
How do I add buttons that work inside the dialog?
Give the dialog's card Action.Submit buttons, and route them with OnSubmit: How to Read the Values Submitted from a Teams Dialog in the Microsoft 365 Agents SDK in C#.
What sizes can a dialog have?
small, medium, large, or pixels: How to Set the Size of a Teams Dialog in the Microsoft 365 Agents SDK in C#.
Conclusion
To open a Teams dialog, give a card's Action.Submit the data "msteams": { "type": "task/fetch" } and a task name, route that name with TeamsExtension.TaskModules.OnFetch, and answer with a Continue response holding the title, the size and the card.
Additional Resources
- Use dialogs with bots, on Microsoft Learn.
- Invoke and dismiss dialogs, on Microsoft Learn: the dialog's properties and sizes.
- TaskModule.OnFetch Method, on Microsoft Learn.
Related
- How to Add the Teams Extension to an Agent in the Microsoft 365 Agents SDK in C#
- How to Read the Values Submitted from a Teams Dialog in the Microsoft 365 Agents SDK in C#
- How to Set the Size of a Teams Dialog in the Microsoft 365 Agents SDK in C#
- Microsoft 365 Agents SDK Fundamentals in C# — Part 10: Teams Dialogs
