Executing Actions
Adaptive Cards support interactive elements through actionsβbuttons, links, and input submission triggers that respond to user interaction. You can use these to collect form input, trigger workflows, show task modules, open URLs, and more.
Action Typesβ
The Teams SDK supports several action types for different interaction patterns:
| Action Type | Purpose | Description |
|---|---|---|
Action.Execute | Serverβside processing | Send data to your bot for processing. Best for forms & multiβstep workflows. |
Action.Submit | Simple data submission | Legacy action type. Prefer Execute for new projects. |
Action.OpenUrl | External navigation | Open a URL in the user's browser. |
Action.ShowCard | Progressive disclosure | Display a nested card when clicked. |
Action.ToggleVisibility | UI state management | Show/hide card elements dynamically. |
For complete reference, see the official documentation.
Creating Actions with the SDKβ
Single Actionsβ
The SDK provides builder helpers that abstract the underlying JSON. For example:
using Microsoft.Teams.Cards;
var action = new ExecuteAction
{
Title = "Submit Feedback",
Data = new Union<string, SubmitActionData>(new SubmitActionData
{
NonSchemaProperties = new Dictionary<string, object?>
{
{ "action", "submit_feedback" }
}
}),
AssociatedInputs = AssociatedInputs.Auto
};
Action Setsβ
Group actions together using ActionSet:
using Microsoft.Teams.Cards;
var card = new AdaptiveCard
{
Schema = "http://adaptivecards.io/schemas/adaptive-card.json",
Actions = new List<Microsoft.Teams.Cards.Action>
{
new ExecuteAction
{
Title = "Submit Feedback",
Data = new Union<string, SubmitActionData>(new SubmitActionData
{
NonSchemaProperties = new Dictionary<string, object?>
{
{ "action", "submit_feedback" }
}
})
},
new OpenUrlAction("https://adaptivecards.microsoft.com")
{
Title = "Learn More"
}
}
};
Raw JSON Alternativeβ
Just like when building cards, if you prefer to work with raw JSON, you can do just that. [Dev] Section "json-safety-note" not found in C# documentation. Either mark the section explicitly as N/A for intentionally ignored, or fill in documentation.
var actionJson = """
{
"type": "Action.OpenUrl",
"url": "https://adaptivecards.microsoft.com",
"title": "Learn More"
}
""";
var action = OpenUrlAction.Deserialize(actionJson);
Working with Input Valuesβ
Associating data with the cardsβ
Sometimes you want to send a card and have it be associated with some data. Set the data value to be sent back to the client so you can associate it with a particular entity.
private static AdaptiveCard CreateProfileCard()
{
return new AdaptiveCard
{
Schema = "http://adaptivecards.io/schemas/adaptive-card.json",
Body = new List<CardElement>
{
new TextBlock("User Profile")
{
Weight = TextWeight.Bolder,
Size = TextSize.Large
},
new TextInput
{
Id = "name",
Label = "Name",
Value = "John Doe"
},
new TextInput
{
Id = "email",
Label = "Email",
Value = "john@contoso.com"
},
new ToggleInput("Subscribe to newsletter")
{
Id = "subscribe",
Value = "false"
}
},
Actions = new List<Microsoft.Teams.Cards.Action>
{
new ExecuteAction
{
Title = "Save",
// entity_id will come back after the user submits
Data = new Union<string, SubmitActionData>(new SubmitActionData
{
NonSchemaProperties = new Dictionary<string, object?>
{
{ "action", "save_profile" },
{ "entity_id", "12345" }
}
}),
AssociatedInputs = AssociatedInputs.Auto
}
}
};
}
// Data received in handler (conceptual structure)
/*
{
"action": "save_profile",
"entity_id": "12345", // From action data
"name": "John Doe", // From name input
"email": "john@doe.com", // From email input
"subscribe": "true" // From toggle input (as string)
}
Accessed in C# as:
- data["action"] β "save_profile"
- data["entity_id"] β "12345"
- data["name"] β "John Doe"
- data["email"] β "john@doe.com"
- data["subscribe"] β "true"
*/
Input Validationβ
Input Controls provide ways for you to validate. More details can be found on the Adaptive Cards documentation.
private static AdaptiveCard CreateProfileCardWithValidation()
{
return new AdaptiveCard
{
Schema = "http://adaptivecards.io/schemas/adaptive-card.json",
Body = new List<CardElement>
{
new TextBlock("Profile with Validation")
{
Weight = TextWeight.Bolder,
Size = TextSize.Large
},
new NumberInput
{
Id = "age",
Label = "Age",
IsRequired = true,
Min = 0,
Max = 120
},
// Can configure custom error messages
new TextInput
{
Id = "name",
Label = "Name",
IsRequired = true,
ErrorMessage = "Name is required"
},
new TextInput
{
Id = "location",
Label = "Location"
}
},
Actions = new List<Microsoft.Teams.Cards.Action>
{
new ExecuteAction
{
Title = "Save",
// All inputs should be validated
Data = new Union<string, SubmitActionData>(new SubmitActionData
{
NonSchemaProperties = new Dictionary<string, object?>
{
{ "action", "save_profile" }
}
}),
AssociatedInputs = AssociatedInputs.Auto
}
}
};
}
Server Handlersβ
Basic Structureβ
Card actions arrive as card.action activities in your app. These give you access to the validated input values plus any data values you had configured to be sent back to you.
using System.Text.Json;
using Microsoft.Teams.Api.Activities.Invokes.AdaptiveCards;
using Microsoft.Teams.Apps;
using Microsoft.Teams.Apps.Annotations;
using Microsoft.Teams.Common.Logging;
//...
[Microsoft.Teams.Apps.Activities.Invokes.AdaptiveCard.Action]
public async Task<ActionResponse> OnCardAction([Context] ActionActivity activity, [Context] IContext.Client client, [Context] ILogger log)
{
log.Info("[CARD_ACTION] Card action received");
var data = activity.Value?.Action?.Data;
if (data == null)
{
log.Error("[CARD_ACTION] No data in card action");
return new ActionResponse.Message("No data specified") { StatusCode = 400 };
}
// Extract action from the Value property
string? action = data.TryGetValue("action", out var actionObj) ? actionObj?.ToString() : null;
if (string.IsNullOrEmpty(action))
{
log.Error("[CARD_ACTION] No action specified in card data");
return new ActionResponse.Message("No action specified") { StatusCode = 400 };
}
log.Info($"[CARD_ACTION] Processing action: {action}");
// Helper method to extract form field values
string? GetFormValue(string key)
{
if (data.TryGetValue(key, out var val))
{
if (val is JsonElement element)
return element.GetString();
return val?.ToString();
}
return null;
}
switch (action)
{
case "submit_feedback":
var feedbackText = GetFormValue("feedback") ?? "No feedback provided";
await client.Send($"Feedback received: {feedbackText}");
break;
case "save_profile":
var name = GetFormValue("name") ?? "Unknown";
var email = GetFormValue("email") ?? "No email";
var subscribe = GetFormValue("subscribe") ?? "false";
await client.Send($"Profile saved!\nName: {name}\nEmail: {email}\nSubscribed: {subscribe}");
break;
case "create_task":
var title = GetFormValue("title") ?? "Untitled";
var priority = GetFormValue("priority") ?? "medium";
var dueDate = GetFormValue("due_date") ?? "No date";
await client.Send($"Task created!\nTitle: {title}\nPriority: {priority}\nDue: {dueDate}");
break;
default:
log.Error($"[CARD_ACTION] Unknown action: {action}");
return new ActionResponse.Message("Unknown action") { StatusCode = 400 };
}
return new ActionResponse.Message("Action processed successfully") { StatusCode = 200 };
}
The data values come from JSON and need to be extracted using the helper method shown above to handle different JSON element types.