Skip to main content

How to Return an Updated Adaptive Card from Action.Execute in the Microsoft 365 Agents SDK in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

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​

C#
AdaptiveCards.OnActionExecute(verb, handler);

private Task<AdaptiveCardInvokeResponse> handler(ITurnContext turnContext, ITurnState turnState, object data, CancellationToken cancellationToken)
{
return Task.FromResult(AdaptiveCardInvokeResponseFactory.AdaptiveCard(cardJson));
}
  • AdaptiveCards.OnActionExecute adds a route for adaptiveCard/action invokes, which is what a chat app sends when someone selects an Action.Execute button.
  • verb is the button's verb. The route matches it exactly. You can also pass a Regex.
  • handler returns a Task<AdaptiveCardInvokeResponse>: the answer to this click. The SDK sends it back for you.
  • data is the button's data, 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 --version should print 0.2.30; if the command isn't found, install it with npm install -g @microsoft/m365agentsplayground.

Step 1: Route the Button and Return the New Card​

TaskAgent.cs
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));
}

TaskAgent.cs in a macOS-style editor window: the task card with a Mark as done Action.Execute button, the done card, the constructor that routes the done verb with AdaptiveCards.OnActionExecute, and MarkDoneAsync returning AdaptiveCardInvokeResponseFactory.AdaptiveCard with the done card

  • The button is an Action.Execute with the verb done. An Action.Execute has its own verb property, so the verb doesn't go in data, where the SDK's routes look for an Action.Submit button's verb.
  • "version": "1.5": Microsoft's Teams guide says a card that uses Action.Execute must be set to 1.5.
  • AdaptiveCards.OnActionExecute("done", MarkDoneAsync) routes the button to MarkDoneAsync.
  • MarkDoneAsync doesn'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:

Terminal 2
agentsplayground -e http://localhost:3978/api/messages -c msteams

In Personal Chat, send task:

The Agents Playground: Test Bot's card Water the plants, Due today, with a Mark as done button

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

The same message, now the card Water the plants with Done in green and no button, and a grey bar under it: Your response was sent to the app

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:

The invoke's value and name (Request tab)
"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:

The Log Panel with the message and invoke entries, and the Response tab of the invoke 200 entry, starting with statusCode 200, type application/vnd.microsoft.card.adaptive and the card in value

The agent's answer (Response tab)
{
"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 its value.action is a copy of the button: its type, title, verb and data.
  • statusCode 200 with type application/vnd.microsoft.card.adaptive means "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​

MistakeWhat happensDo this instead
Putting the verb in data, as with Action.SubmitThe route never matchesGive the Action.Execute its own verb property
A card version below 1.5Microsoft's Teams guide: Your card stops working in TeamsUse "version": "1.5"
Registering the verb with OnActionSubmitAction.Execute arrives as an invoke, not a message, so the route never runsUse OnActionExecute

Troubleshooting​

SymptomLikely causeFix
Nothing happens when you select the buttonNo OnActionExecute route for the verbCompare the button's verb and the route's, case included
The card doesn't changeThe handler returned a message or an error instead of a cardReturn 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​