How to Show an Error in a Teams Dialog in the Microsoft 365 Agents SDK in C#
Teams checks a dialog's required fields by itself, but only your agent knows its own rules: a room that holds 8 people, a budget, a date that's already taken. When a rule fails, the dialog should stay open and say what to fix, without making the person type everything again. In the Microsoft 365 Agents SDK, your agent answers the submit with the same form, the problem and the values.
Syntax
In the dialog's OnSubmit handler:
string value = request.GetDataString("<input id>");
if (<the value breaks a rule>)
{
// The same form again, with the problem and the typed value.
return Task.FromResult(<Form>(value, "<what to fix>"));
}
// Otherwise, finish: a message, no answer, or the next step.
| Part | What it does |
|---|---|
request.GetDataString("<input id>") | The value as the person typed it, as text |
<Form>(value, "<what to fix>") | Your method that builds the form, with each input's value set to what was typed, and the problem in a TextBlock above |
Returning it as Continue | Teams replaces the dialog's card with this one, so the dialog stays open |
A Continue answer to a submit is how Microsoft's Use dialogs with bots chains cards in a dialog. Sending the same form back is the shortest chain: the person stays on the same step.
The Example
The agent for this post books meeting rooms that hold up to 8 people. Its dialog asks How many people? and has a Book button. For 12 people, the form comes back with Our rooms hold up to 8 people. Book two rooms, or the auditorium. in red, and 12 still in the box. For 6, the dialog ends with Room 3.14 is yours, for 6 people.
Prerequisites
- An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.
Step 1: Check the Value, and Send the Form Back
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 HoError;
[TeamsExtension]
public partial class RoomAgent : AgentApplication
{
private const int MaxPeople = 8;
public RoomAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("room", OpenAsync);
TeamsExtension.TaskModules.OnSubmit("room", BookAsync);
}
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"] = "Meeting rooms", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "Book a room on the third floor.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Book a room",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "room",
},
},
},
};
return turnContext.SendActivityAsync(
MessageFactory.Attachment(new Attachment { ContentType = ContentTypes.AdaptiveCard, Content = card }),
cancellationToken);
}
private Task<TaskModuleResponse> OpenAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(RoomForm(people: "2", error: null));
}
// task/submit: check the number. Too many people: the same form again, with the problem and the number they typed.
private Task<TaskModuleResponse> BookAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
string people = request.GetDataString("people");
if (!int.TryParse(people, out int count) || count > MaxPeople)
{
return Task.FromResult(RoomForm(people, $"Our rooms hold up to {MaxPeople} people. Book two rooms, or the auditorium."));
}
TaskModuleResponse booked = new()
{
Task = new Response { Type = TaskModuleResponseTypes.Message, Value = $"Room 3.14 is yours, for {count} people." },
};
return Task.FromResult(booked);
}
// The form, showing people as the number and error, when there is one, in red above it.
private static TaskModuleResponse RoomForm(string people, string? error)
{
JsonArray body = new();
if (error is not null)
{
body.Add(new JsonObject { ["type"] = "TextBlock", ["text"] = error, ["color"] = "Attention", ["wrap"] = true });
}
body.Add(new JsonObject
{
["type"] = "Input.Number",
["id"] = "people",
["label"] = "How many people?",
["min"] = 1,
["isRequired"] = true,
["errorMessage"] = "Enter how many people, 1 or more.",
["value"] = int.TryParse(people, out int value) ? value : 2,
});
JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = body,
["actions"] = new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Book", ["data"] = new JsonObject { ["task"] = "room" } },
},
};
return new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title = "Book a meeting room",
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};
}
}

RoomForm(people, error)builds the form every time: when the dialog opens, with no error, and after a problem, with it.["value"] = ...puts the number back in the box. Card values arrive as text, so it's parsed back to a number for theInput.Number.["color"] = "Attention"shows the problem in the host's warning color: red, in Teams.BookAsyncchecks the number withint.TryParseand the limit, and returns either the form again or the final message.
Step 2: Book for Too Many People
Send the agent any message, select Book a room, and enter 12:

Select Book:

The dialog stayed open, under the same title, with the problem on top and 12 still in the box. The submit Teams sent was {"task": "room", "people": "12"}: the number arrived as text.
Step 3: Book a Room That Fits
Change the number to 6, and select Book:

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Sending back the empty form | The person types everything again | Set each input's value to what was typed |
Answering a problem with a Message | The form is gone, and the person has to start over | Answer with the form, as Continue |
| Trusting Teams' checks alone | Rules only the agent knows go unchecked | Check in the agent too |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The error shows, but the box is empty | The form wasn't given the typed value | Pass it to the form and set value |
| The number box shows the old default | The text value didn't parse, so the default was used | Parse it with int.TryParse, as RoomForm does |
| The dialog closes instead | The handler returned a message or null | Return the form with Continue |
FAQ
Why not use the input's errorMessage?
errorMessage is shown by Teams for its own checks, such as a required input left empty, before anything reaches your agent. A rule only your agent knows, such as the room size, has to come back from the agent.
Can I check the number with max instead?
For a fixed limit, max on the Input.Number keeps the number in range in Teams, which changes a bigger number to the maximum when the person leaves the box. Your agent should still check: Fixed: An Adaptive Card Number Input in Teams Changes to Its Maximum Instead of Showing the Error Message.
Conclusion
To show an error in a Teams dialog, check the submitted values in the OnSubmit handler, and when a rule fails, answer with the same form as Continue: the problem in a red TextBlock on top, and each input's value set to what was typed.
Additional Resources
- Use dialogs with bots, on Microsoft Learn.
- TextBlock, on Microsoft Learn:
color. - Input.Number, on Microsoft Learn:
value.
Related
- How to Read the Values Submitted from a Teams Dialog in the Microsoft 365 Agents SDK in C#
- How to Build a Multi-Step Teams Dialog in the Microsoft 365 Agents SDK in C#
- How to Prefill the Inputs of an Adaptive Card
- How to Change the Color of Text in an Adaptive Card
- Microsoft 365 Agents SDK Fundamentals in C# — Part 10: Teams Dialogs
