How to Close a Teams Dialog Without a Message in the Microsoft 365 Agents SDK in C#
Sometimes a dialog's button should simply close it: Got it, Cancel, or a form whose result your agent posts in the chat. Teams closes a dialog when the agent's answer to the button is no answer at all, and in the Microsoft 365 Agents SDK that's a handler that returns null.
Syntax
TeamsExtension.TaskModules.OnSubmit("<name>", handler);
// In the handler:
return Task.FromResult<TaskModuleResponse>(null!);
| Part | What it does |
|---|---|
OnSubmit("<name>", handler) | Routes the dialog button whose data has "task": "<name>" |
null | No answer: the SDK sends Teams an HTTP 200 with no body, and Teams closes the dialog |
! after null | Tells the C# compiler the null is intended. The SDK's handler type says the answer isn't null, so without it the build warns |
Microsoft's Use dialogs with bots lists it first among the answers: The simplest response is no response at all. Your bot isn't required to respond when the user finishes with the dialog. In an async handler, write return null!;.
The Example
The agent for this post announces a new version of an app. Its card's What's new button opens a dialog with three changes and a Got it button, which closes the dialog.
Prerequisites
- An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.
Step 1: Return No Answer
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 HoClose;
[TeamsExtension]
public partial class ReleaseNotesAgent : AgentApplication
{
public ReleaseNotesAgent(AgentApplicationOptions options) : base(options)
{
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("whatsnew", OpenAsync);
TeamsExtension.TaskModules.OnSubmit("whatsnew", CloseAsync);
}
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"] = "Expense app 2.4", ["size"] = "Large", ["weight"] = "Bolder" },
new JsonObject { ["type"] = "TextBlock", ["text"] = "The new version is out.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "What's new",
["data"] = new JsonObject
{
["msteams"] = new JsonObject { ["type"] = "task/fetch" },
["task"] = "whatsnew",
},
},
},
};
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)
{
JsonObject card = new()
{
["type"] = "AdaptiveCard",
["version"] = "1.5",
["body"] = new JsonArray
{
new JsonObject { ["type"] = "TextBlock", ["text"] = "- Scan a receipt with your phone's camera.\n- See your last ten claims on the start page.\n- Claims under 20 dollars are approved at once.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject { ["type"] = "Action.Submit", ["title"] = "Got it", ["data"] = new JsonObject { ["task"] = "whatsnew" } },
},
};
TaskModuleResponse response = new()
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new
{
title = "What's new in 2.4",
width = "medium",
height = "small",
card = new TeamsAttachment { ContentType = AttachmentContentTypes.AdaptiveCard, Content = card },
},
},
};
return Task.FromResult(response);
}
// task/submit: no answer at all, so Teams closes the dialog and shows nothing.
// The handler's type promises a response; the SDK accepts null as "no answer", and null! tells C# that's intended.
private Task<TaskModuleResponse> CloseAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult<TaskModuleResponse>(null!);
}
}

OpenAsyncopens the dialog, as in How to Open a Teams Dialog from an Adaptive Card in the Microsoft 365 Agents SDK in C#. Its Got it button sends"task": "whatsnew".CloseAsyncroutes that button and returnsnull!.
The ! is there because the SDK's TaskSubmitHandler returns Task<TaskModuleResponse>, without ?. Plain null builds with warning CS8625: Cannot convert null literal to non-nullable reference type, and the SDK's code accepts the null anyway.
Step 2: Close It in Teams
Send the agent any message, and select What's new:

Select Got it: the dialog closed, and nothing else appeared. The agent's log showed its answer to the button:
{
"status": 200
}
A 200 with no body: the SDK's code turns a null answer into exactly this, and Teams closes the dialog.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
return null; without ! | Warning CS8625 | null! |
Changing the handler's return type to Task<TaskModuleResponse?> | Warning CS8621 where the handler is registered | Keep Task<TaskModuleResponse>, and return null! |
| A Cancel button whose inputs are still checked | Teams asks for the required inputs before sending anything | Add "associatedInputs": "none" to the button |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The dialog stays open | The handler returned a Continue or Message answer | Return null! |
| Unable to reach app. Please try again. | No route matched the button's task, or the agent isn't running | Check the agent's log for the invoke |
FAQ
Does the X send anything to the agent?
No. In my tests, closing a dialog with its X sent the agent nothing at all. Only the dialog's own buttons reach the agent.
Can I do something before closing?
Yes. Anything in the handler runs first, such as posting a message in the chat with turnContext.SendActivityAsync. The dialog closes when the handler returns null.
Conclusion
To close a Teams dialog from your agent with nothing more to show, return null! from the button's OnSubmit handler. The SDK sends Teams an empty 200 answer, and Teams closes the dialog.
Additional Resources
- Use dialogs with bots, on Microsoft Learn: the answers to a dialog's submit.
- ! (null-forgiving) operator, on Microsoft Learn.
Related
- 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
- Fixed: warning CS8625: Cannot convert null literal to non-nullable reference type
- Microsoft 365 Agents SDK Fundamentals in C# — Part 10: Teams Dialogs
