How to Return an Updated Adaptive Card from Action.Execute in the Microsoft 365 Agents SDK in C#
An Action.Execute button on an Adaptive Card gets an answer from your agent, and the answer can be a new card that takes the old one's place. In the Microsoft 365 Agents SDK, you route the button's verb to a handler and return the new card from it.
Syntax
AdaptiveCards.OnActionExecute(verb, handler);
private Task<AdaptiveCardInvokeResponse> handler(ITurnContext turnContext, ITurnState turnState, object data, CancellationToken cancellationToken)
{
return Task.FromResult(AdaptiveCardInvokeResponseFactory.AdaptiveCard(cardJson));
}
AdaptiveCards.OnActionExecuteadds a route foradaptiveCard/actioninvokes, which is what a chat app sends when someone selects anAction.Executebutton.verbis the button'sverb. The route matches it exactly. You can also pass aRegex.handlerreturns aTask<AdaptiveCardInvokeResponse>: the answer to this click. The SDK sends it back for you.datais the button'sdata, plus the card's inputs, if it has any.AdaptiveCardInvokeResponseFactory.AdaptiveCard(cardJson)builds the answer that replaces the card, from the new card's JSON as a string.
Microsoft's guide to adaptive cards in agents: An Action.Execute handler returns an AdaptiveCardInvokeResponse. Use AdaptiveCardInvokeResponseFactory to return a message, replacement card, sign-in card, search response, or error response.
The Example
The agent for this post answers task with a card for one chore, Water the plants, due today, with a Mark as done button. Selecting the button turns the card into the same chore marked Done, with no button. Both cards are small, so they live in the code as C# raw string literals. The agent is one class in an ASP.NET Core project with the Agents SDK, hosted with AddAgent<TaskAgent>() and listening on port 3978.
Prerequisites
- The Microsoft 365 Agents Playground, to chat with the agent on your own machine.
agentsplayground --versionshould print0.2.30; if the command isn't found, install it withnpm install -g @microsoft/m365agentsplayground.
Step 1: Route the Button and Return the New Card
using System.Text.Json.Nodes;
using Microsoft.Agents.Builder;
using Microsoft.Agents.Builder.App;
using Microsoft.Agents.Builder.App.AdaptiveCards;
using Microsoft.Agents.Builder.State;
using Microsoft.Agents.Core.Models;
namespace TaskCard;
public class TaskAgent : AgentApplication
{
private const string TaskCardJson = """
{
"type": "AdaptiveCard",
"version": "1.5",
"body": [
{ "type": "TextBlock", "text": "Water the plants", "size": "Large", "weight": "Bolder" },
{ "type": "TextBlock", "text": "Due today", "isSubtle": true }
],
"actions": [
{ "type": "Action.Execute", "title": "Mark as done", "verb": "done" }
]
}
""";
private const string DoneCardJson = """
{
"type": "AdaptiveCard",
"version": "1.5",
"body": [
{ "type": "TextBlock", "text": "Water the plants", "size": "Large", "weight": "Bolder" },
{ "type": "TextBlock", "text": "Done", "color": "Good", "weight": "Bolder" }
]
}
""";
public TaskAgent(AgentApplicationOptions options) : base(options)
{
OnMessage("task", SendTaskAsync);
AdaptiveCards.OnActionExecute("done", MarkDoneAsync);
}
private Task SendTaskAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
Attachment card = new() { ContentType = ContentTypes.AdaptiveCard, Content = JsonNode.Parse(TaskCardJson) };
return turnContext.SendActivityAsync(MessageFactory.Attachment(card), cancellationToken);
}
private Task<AdaptiveCardInvokeResponse> MarkDoneAsync(ITurnContext turnContext, ITurnState turnState, object data, CancellationToken cancellationToken)
=> Task.FromResult(AdaptiveCardInvokeResponseFactory.AdaptiveCard(DoneCardJson));
}

- The button is an
Action.Executewith the verbdone. AnAction.Executehas its ownverbproperty, so the verb doesn't go indata, where the SDK's routes look for anAction.Submitbutton's verb. "version": "1.5": Microsoft's Teams guide says a card that usesAction.Executemust be set to 1.5.AdaptiveCards.OnActionExecute("done", MarkDoneAsync)routes the button toMarkDoneAsync.MarkDoneAsyncdoesn't send anything. It returns the answer, a card, and that card replaces the one the person used.
Step 2: Try It
Run the agent with dotnet run, then start the Agents Playground in a second terminal, on its Teams channel:
agentsplayground -e http://localhost:3978/api/messages -c msteams
In Personal Chat, send task:

Select Mark as done. The card changes in place, and no new message appears:

The grey bar, Your response was sent to the app, comes from the Playground itself, not from your agent. Teams showed the same note in this series' tests under a returned card that had no buttons.
Step 3: See the Answer
In the Log Panel, select the invoke 200 entry. The Request tab shows what the button sent:
"value": {
"action": {
"type": "Action.Execute",
"title": "Mark as done",
"verb": "done",
"data": {}
}
},
"name": "adaptiveCard/action",
"channelId": "msteams"
The Response tab shows your agent's answer:

{
"statusCode": 200,
"type": "application/vnd.microsoft.card.adaptive",
"value": {
"type": "AdaptiveCard",
"version": "1.5",
"body": [
{
"type": "TextBlock",
"text": "Water the plants",
"size": "Large",
"weight": "Bolder"
},
{
"type": "TextBlock",
"text": "Done",
"color": "Good",
"weight": "Bolder"
}
]
}
}
- The invoke is named
adaptiveCard/action, and itsvalue.actionis a copy of the button: itstype,title,verbanddata. statusCode200 withtypeapplication/vnd.microsoft.card.adaptivemeans "display this card". Microsoft's Universal Action model: the response includes an Adaptive Card that the client should display in place of the current one.- The card is JSON, not a string, even though
AdaptiveCard()took a string. The SDK writes a string that holds a JSON object as the object itself.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Putting the verb in data, as with Action.Submit | The route never matches | Give the Action.Execute its own verb property |
A card version below 1.5 | Microsoft's Teams guide: Your card stops working in Teams | Use "version": "1.5" |
Registering the verb with OnActionSubmit | Action.Execute arrives as an invoke, not a message, so the route never runs | Use OnActionExecute |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing happens when you select the button | No OnActionExecute route for the verb | Compare the button's verb and the route's, case included |
| The card doesn't change | The handler returned a message or an error instead of a card | Return AdaptiveCard(...) |
FAQ
Who sees the new card?
Only the person who selected the button. The answer to an invoke goes to the client that sent it. To change the card for everyone in a chat, update the message itself with UpdateActivityAsync.
Can the handler read the card's inputs?
Yes: data holds the button's data and the value of every input, under each input's id, as with Action.Submit. Read them into a record with ProtocolJsonSerializer.ToObject<T>(data).
Can I return a card object instead of a string?
The factory method takes a string. You can also build an AdaptiveCardInvokeResponse yourself, with StatusCode 200, Type application/vnd.microsoft.card.adaptive and the card as Value.
Conclusion
To replace a card when someone selects its button, make the button an Action.Execute with a verb, route the verb with AdaptiveCards.OnActionExecute, and return AdaptiveCardInvokeResponseFactory.AdaptiveCard with the new card's JSON. The new card takes the old one's place, for the person who selected the button.
Additional Resources
- Use adaptive cards in agents, on Microsoft Learn:
OnActionExecuteand the factory. - Universal Action Model, on Microsoft Learn: the invoke and every answer it can get.
- AdaptiveCardInvokeResponseFactory class, on Microsoft Learn.
