How to Submit a Web Page Dialog to Your Agent in the Microsoft 365 Agents SDK in C#
A web page in a Teams dialog can send what someone typed to your agent. One call to the Teams JavaScript library (TeamsJS) on the page sends it, and the agent receives it as a task/submit invoke, the same kind a card dialog's button sends.
Syntax
On the page, after microsoftTeams.app.initialize():
microsoftTeams.dialog.url.submit(<result>, "<app ID>");
In the agent:
TeamsExtension.TaskModules.OnSubmit("<task name>", handler);
// In the handler:
string value = request.GetDataString("<key>", "<default>");
| Part | What it does |
|---|---|
<result> | What the page sends: an object or a string. As an object, give it a task property with the name the agent routes on |
"<app ID>" | Your Teams app's ID: the id at the top of its manifest.json |
OnSubmit("<task name>", handler) | Calls handler for a task/submit whose data has "task": "<task name>" |
GetDataString("<key>", "<default>") | Reads one property of the result as text, or the default when it's missing |
Microsoft's Use dialogs in tabs explains the two arguments: you can pass an object or string result back to your app as the first parameter, and an appId of the app that opened the dialog as the second parameter. If you call submit() with a result parameter, you must pass an appId (or an array of appId strings of apps authorized to receive the result of the dialog). This action enables Teams to validate that the app sending the result is the same as the invoked dialog.
The TeamsJS reference adds why: Specifying this parameter helps prevent malicious apps from retrieving the dialog result.
Microsoft's Use dialogs with bots still shows the older call, microsoftTeams.tasks.submitTask(). The reference for it says: This API is now deprecated. As of 2.8.0, please use dialog.url.submit instead.
The Example
The agent for this post runs an office suggestion box. Any message gets a card with a Make a suggestion button, which opens a dialog with the agent's page, suggest.html: a text box and a Send button. Send passes the suggestion to the agent, which thanks the person in the chat and closes the dialog.
Prerequisites
- An agent that already runs in Microsoft Teams, in a personal chat, with the Teams extension added.
- The agent serving its
wwwrootfolder withapp.MapStaticAssets(), at a public HTTPS address such as a dev tunnel's. - That address's host in your app's
validDomains, so Teams loads the page in a dialog.
Step 1: Build the Form
Create wwwroot/suggest.html. Start with the form in the <body>:
<form id="suggestion-form">
<label for="suggestion">Your suggestion</label>
<textarea id="suggestion" rows="4" required></textarea>
<button type="submit">Send</button>
</form>
<label for="suggestion">names the text box. Itsformatches the box'sid.<textarea>is a text box with more than one line.rows="4"makes it four lines high, andrequiredmarks it as one that must be filled in: the browser checks it before the form is sent.<button type="submit">sends the form.
Step 2: Send the Result to the Agent
Below the TeamsJS script tag, connect to Teams, then handle the form's submit event:
<script>
// Your Teams app's ID, from its manifest.
const appId = "22222222-2222-2222-2222-222222222222";
async function start() {
await microsoftTeams.app.initialize();
document.getElementById("suggestion-form").addEventListener("submit", send);
}
// Teams sends the object to the agent as a task/submit invoke.
function send(event) {
event.preventDefault();
const suggestion = document.getElementById("suggestion").value;
microsoftTeams.dialog.url.submit({ task: "suggestion", suggestion }, appId);
}
start();
</script>
appIdis your Teams app's ID. Copy theidfrom your app'smanifest.jsonin place of the placeholder.await microsoftTeams.app.initialize()connects the page to Teams first. Only then does the form get its handler, so a Send always reaches a connected page.event.preventDefault()stops the browser's own handling of the form, which would send it to the page's address and load that address again. TeamsJS sends the result instead.{ task: "suggestion", suggestion }is the result: thetaskname the agent routes on, and the text.suggestionon its own is short forsuggestion: suggestion.microsoftTeams.dialog.url.submit(…, appId)hands the result and the app's ID to Teams.
How to Add the Teams JavaScript Library (TeamsJS) to a Web Page from the CDN explains the script tag that loads TeamsJS. The whole page:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Suggestion box</title>
<style>
body { margin: 0; padding: 0 24px; font-family: "Segoe UI", system-ui, sans-serif; font-size: 14px; }
label { display: block; margin-bottom: 6px; }
textarea { width: 100%; box-sizing: border-box; font: inherit; }
button { margin-top: 12px; padding: 5px 20px; font: inherit; }
</style>
</head>
<body>
<form id="suggestion-form">
<label for="suggestion">Your suggestion</label>
<textarea id="suggestion" rows="4" required></textarea>
<button type="submit">Send</button>
</form>
<script src="https://res.cdn.office.net/teams-js/2.60.0/js/MicrosoftTeams.min.js"
integrity="sha384-oM1pbPWSxyAWgEVI9GzHDR2xzdMMehbAdsl9cH5emcTxX9Qr/kE4IS/Ld5IdcLmy"
crossorigin="anonymous"></script>
<script>
// Your Teams app's ID, from its manifest.
const appId = "22222222-2222-2222-2222-222222222222";
async function start() {
await microsoftTeams.app.initialize();
document.getElementById("suggestion-form").addEventListener("submit", send);
}
// Teams sends the object to the agent as a task/submit invoke.
function send(event) {
event.preventDefault();
const suggestion = document.getElementById("suggestion").value;
microsoftTeams.dialog.url.submit({ task: "suggestion", suggestion }, appId);
}
start();
</script>
</body>
</html>
Step 3: Route the Result in the Agent
The agent opens the dialog with the page, as How to Show a Web Page in a Teams Dialog in the Microsoft 365 Agents SDK in C# shows, and gets one more route, OnSubmit, for the result:
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.TaskModules;
namespace SuggestionBox;
[TeamsExtension]
public partial class SuggestionBoxAgent : AgentApplication
{
private readonly string _pageUrl;
public SuggestionBoxAgent(AgentApplicationOptions options, IConfiguration configuration)
: base(options)
{
// PublicUrl is the agent's public HTTPS address: the dev tunnel's. The page is wwwroot/suggest.html.
_pageUrl = new Uri(new Uri(configuration["PublicUrl"]!), "suggest.html").ToString();
OnActivity(ActivityTypes.Message, SendCardAsync);
TeamsExtension.TaskModules.OnFetch("suggestions", OpenPageAsync);
TeamsExtension.TaskModules.OnSubmit("suggestion", SaveSuggestionAsync);
}
// Any message gets a card whose button opens the dialog.
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"] = "Got an idea for the office? Drop it in the suggestion box.", ["wrap"] = true },
},
["actions"] = new JsonArray
{
new JsonObject
{
["type"] = "Action.Submit",
["title"] = "Make a suggestion",
["data"] = new JsonObject { ["msteams"] = new JsonObject { ["type"] = "task/fetch" }, ["task"] = "suggestions" },
},
},
};
return turnContext.SendActivityAsync(MessageFactory.Attachment(new Attachment { ContentType = ContentTypes.AdaptiveCard, Content = card }), cancellationToken);
}
// task/fetch: the page's address. Teams opens the dialog and loads the page in it.
private Task<TaskModuleResponse> OpenPageAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new { title = "Suggestion box", width = 400, height = 230, url = _pageUrl },
},
});
}
// task/submit: what the page passed to dialog.url.submit, with "task": "suggestion".
private async Task<TaskModuleResponse> SaveSuggestionAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
string suggestion = request.GetDataString("suggestion", "");
await turnContext.SendActivityAsync($"Thanks! Your suggestion: {suggestion}", cancellationToken: cancellationToken);
// No answer: Teams closes the dialog.
return null!;
}
}
OnFetch("suggestions", OpenPageAsync)opens the dialog with the page, from the card's Make a suggestion button.OnSubmit("suggestion", SaveSuggestionAsync)routes the page's result: itstaskissuggestion. The dialog's name and the result's name differ, so each route is easy to tell apart.request.GetDataString("suggestion", "")reads the result'ssuggestionproperty, or an empty string when it's missing. How to Read the Values Submitted from a Teams Dialog in the Microsoft 365 Agents SDK in C# explains it and its sibling,GetDataAs.return null!sends no answer, so Teams closes the dialog.
Step 4: Send a Suggestion in Teams
Replace the placeholder with your app's ID, start the agent with dotnet run, and send it any message in Teams. Select Make a suggestion on the card, and type a suggestion:

Select Send. The dialog closed, and the agent's reply arrived in the chat:

What Teams Sent
The agent's log showed the task/submit invoke. Its value:
{
"data": {
"task": "suggestion",
"suggestion": "Put a water cooler on the third floor."
},
"context": {
"theme": "default"
}
}
data is exactly the object the page passed to submit(). The agent's answer was {"status":200}, with no body: the "no answer" that closes the dialog.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling submit() with a result and no app ID | Microsoft's docs require an appId with every result | Pass your app's ID as the second argument |
| Leaving the placeholder app ID in the page | The page doesn't name your app | Copy the id from your manifest.json |
A different task value in the page and in OnSubmit | No route matches the invoke | Use one name in both places |
Using microsoftTeams.tasks.submitTask() | It's deprecated as of TeamsJS 2.8.0 | Use microsoftTeams.dialog.url.submit() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Send does nothing | The script stopped before the form got its handler, for example because TeamsJS didn't load | Open the browser console and read the first error |
| The agent replies with an empty suggestion | The result has no suggestion property, so GetDataString returned the default | Check the property name in the page's object |
No task/submit in the agent's log | The page's script didn't call submit() | Check the console for errors in the handler |
FAQ
Can the page send a string instead of an object?
Microsoft's docs allow an object or string result. An object is easier to route: its task property is what OnSubmit matches.
Can the agent keep the dialog open after the submit?
Yes. Answer with another Continue response, and the dialog shows what it holds, such as the next page: How to Show the Next Web Page in a Teams Dialog in the Microsoft 365 Agents SDK in C#.
How do I close the dialog without sending anything?
Call microsoftTeams.dialog.url.submit() with no arguments: How to Close a Teams Dialog from Its Web Page with TeamsJS.
Which ID is the app ID?
The id at the top of your app's manifest.json, the one Teams knows your app by.
Conclusion
To send a web page dialog's result to your agent, call microsoftTeams.dialog.url.submit() with an object that holds a task name and your values, and your app's ID as the second argument. Route the task/submit invoke with TeamsExtension.TaskModules.OnSubmit, read the values with GetDataString, and return null! to close the dialog.
Additional Resources
- Use dialogs in tabs: Submit the result of a dialog, on Microsoft Learn.
- dialog.url module of TeamsJS, on Microsoft Learn.
- TaskModule.OnSubmit Method, on Microsoft Learn.
Related
- How to Show a Web Page in a Teams Dialog in the Microsoft 365 Agents SDK in C#
- How to Show the Next Web Page in a Teams Dialog in the Microsoft 365 Agents SDK in C#
- How to Read the Values Submitted from a Teams Dialog in the Microsoft 365 Agents SDK in C#
- How to Close a Teams Dialog from Its Web Page with TeamsJS
- Microsoft 365 Agents SDK Fundamentals in C# — Part 11: Show a Web Page in a Teams Dialog
