How to Read the Values Submitted from a Teams Dialog in the Microsoft 365 Agents SDK in C#
A form in a Teams dialog is only useful once your agent reads what was typed. When someone selects a button in the dialog, Teams sends the agent everything in one invoke, and the Microsoft 365 Agents SDK hands your handler each value by its input's id.
Syntax
The button, in the dialog's card:
{ "type": "Action.Submit", "title": "<button text>", "data": { "task": "<name>" } }
The route and the values, in the agent:
TeamsExtension.TaskModules.OnSubmit("<name>", handler);
// In the handler:
string value = request.GetDataString("<input id>");
| Part | What it does |
|---|---|
"task": "<name>" | The name your agent routes on. The dialog's Action.Submit needs no msteams object |
OnSubmit("<name>", handler) | Calls handler when someone selects that button in a dialog |
request | The TaskModuleRequest the handler gets. request.Data holds the button's data and every input's value |
GetDataString("<input id>") | One value, by the input's id, as text. An optional second argument is the value to use when it's missing |
The handler returns what the dialog does next, as for OnFetch. Microsoft's Use dialogs with bots lists three answers: no response, a message, or another card. This post uses the first: the dialog closes.
The Example
The agent for this post orders visitor badges. Its card's Order a badge button opens a dialog with one required input, Visitor's name, and an Order button. The agent reads the name, says in the chat that the badge will be ready, and closes the dialog.
Prerequisites
- An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.
Step 1: Route the Dialog's Button
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 HoSubmit;
[TeamsExtension]
public partial class BadgeAgent : AgentApplication
{
public BadgeAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("badge", OpenBadgeFormAsync);
TeamsExtension.TaskModules.OnSubmit("badge", OrderBadgeAsync);
}
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"] = "Visitor badges", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Expecting a visitor? Order a badge, and the front desk prints it.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Order a badge",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "badge",
},
},
},
};
return turnContext.SendActivityAsync(
MessageFactory.Attachment(new Attachment { ContentType = ContentTypes.AdaptiveCard, Content = card }),
cancellationToken);
}
// task/fetch: the dialog with one input. Its button sends "task": "badge" with the input's value.
private Task<TaskModuleResponse> OpenBadgeFormAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = new JsonArray
{
new JsonObject
{
["type"] = "Input.Text",
["id"] = "visitor",
["label"] = "Visitor's name",
["isRequired"] = true,
["errorMessage"] = "Enter the visitor's name.",
},
},
["actions"] = new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Order", ["data"] = new JsonObject { ["task"] = "badge" } },
},
};
TaskModuleResponse response = new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title = "Order a visitor badge",
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};
return Task.FromResult(response);
}
// task/submit: read the input by its id, answer in the chat, and close the dialog.
private async Task<TaskModuleResponse> OrderBadgeAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
string visitor = request.GetDataString("visitor");
await turnContext.SendActivityAsync($"A badge for **{visitor}** will be ready at the front desk.", cancellationToken: cancellationToken);
return null!;
}
}

OnFetch("badge", ...)opens the dialog, the way How to Open a Teams Dialog from an Adaptive Card in the Microsoft 365 Agents SDK in C# shows.OnSubmit("badge", OrderBadgeAsync)routes the dialog's Order button, whose data also says"task": "badge". Fetch and submit are separate routes, so one name can serve both.request.GetDataString("visitor")reads the input whoseidisvisitor.turnContext.SendActivityAsyncposts a message in the chat while the dialog is still open: the handler answers Teams only when it returns.return null!;gives Teams no answer, so it closes the dialog: How to Close a Teams Dialog Without a Message in the Microsoft 365 Agents SDK in C# explains the!.
Step 2: Fill In the Dialog
Send the agent any message, select Order a badge, and type a name:

Select Order. The dialog closed, and the agent's message appeared under the card:

What Teams Sent
The task/submit invoke's value, from the agent's log:
{
"data": {
"task": "badge",
"visitor": "Ana Silva"
},
"context": {
"theme": "default"
}
}
data holds the button's own data, "task": "badge", merged with each input under its id. That's what GetDataString reads. The agent's answer was an empty {"status":200}, the "no response" answer.
Read Everything into a Class
GetDataAs<T>() turns the whole data object into a type of your own. With a record that has a property for each key:
public record BadgeOrder(string Task, string Visitor);
BadgeOrder order = request.GetDataAs<BadgeOrder>();
In this post's agent, that gave BadgeOrder { Task = badge, Visitor = Ana Silva }: the lowercase keys matched the properties' names.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Reading a key that isn't in data | GetDataString returns an empty string | Pass a default as the second argument, or check for "" |
Expecting numbers or true/false | Card inputs send their values as text | Parse the text, for example with int.TryParse |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The dialog doesn't close | The handler returned a Continue answer | Return null!, or a message |
| A value is empty | The input's id and the key you read differ | Use the same id |
FAQ
Can I post in the chat and keep the dialog open?
Yes: SendActivityAsync posts in the chat, and what the dialog does next depends only on what the handler returns. A handler that posts in the chat and answers with a message leaves the dialog open, showing that message, until the person closes it.
What if the person closes the dialog with X?
The agent gets nothing. Only the dialog's own buttons send task/submit.
Does Teams check required inputs first?
Yes. In my tests, a required drop-down list left empty showed its errorMessage in the dialog, and Teams sent the agent nothing until it was filled in.
Conclusion
Route the dialog's button with TeamsExtension.TaskModules.OnSubmit, read each input with request.GetDataString("<id>"), or all of them with GetDataAs<T>(), and return what the dialog does next. To just close it, return null!.
Additional Resources
- Use dialogs with bots, on Microsoft Learn.
- TaskModule.OnSubmit Method, on Microsoft Learn.
Related
- How to Open a Teams Dialog from an Adaptive Card 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 Close a Teams Dialog Without a Message in the Microsoft 365 Agents SDK in C#
- How to Show an Error in a Teams Dialog in the Microsoft 365 Agents SDK in C#
- Microsoft 365 Agents SDK Fundamentals in C# — Part 10: Teams Dialogs
