Skip to main content

Fixed: "theme": "default" in a Teams task/fetch Request While Teams Is in Dark Theme

· 11 min read
Jagdish Kumawat
Founder @ Dewiride

My agent's Teams dialog page took its colors from the theme Teams sends in the task/fetch request. With Teams in Dark theme, that request still said "theme": "default", so the page showed dark text on Teams' dark dialog. The fix is to read the theme in the page itself, with the Teams JavaScript library.

What I Was Doing​

An agent built with the Microsoft 365 Agents SDK in C# sends a card with a Show the menu button. The button opens a Teams dialog that shows lunch.html, a page the agent serves itself.

When someone selects the button, Teams sends the agent a task/fetch request, and the agent answers with the dialog's title, size and page address. That request also carries a theme, which the SDK gives the handler as request.Context?.Theme. To match Teams' colors, the agent put that theme in the page's address:

LunchMenuAgent.cs (part)
// task/fetch: Teams sends the person's theme with the request. The page picks its colors from it.
private Task<TaskModuleResponse> OpenPageAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
string theme = request.Context?.Theme ?? "default";
_logger.LogInformation("task/fetch theme: {Theme}", theme);

return Task.FromResult(new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new { title = "Lunch menu", width = 400, height = 230, url = QueryHelpers.AddQueryString(_pageUrl, "theme", theme) },
},
});
}
  • request.Context?.Theme is the theme from the request, or null if there's none, in which case the agent uses default.
  • _logger.LogInformation(...) writes the theme to the agent's output.
  • QueryHelpers.AddQueryString(_pageUrl, "theme", theme) adds it to the page's address, as in lunch.html?theme=dark.

The page read the theme from its address and wrote it on its body, where the CSS picks a text color for each theme:

wwwroot/lunch.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lunch menu</title>
<style>
body { margin: 0; padding: 0 24px; font-family: "Segoe UI", system-ui, sans-serif; font-size: 14px; color: #242424; }
body[data-theme="dark"],
body[data-theme="contrast"] { color: #ffffff; }
</style>
</head>
<body>
<p>Today's lunch: vegetable curry with rice.</p>
<p>Theme: <strong id="theme">...</strong></p>

<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>
microsoftTeams.app.initialize();

// The theme the agent put in the page's address, from Teams' task/fetch request.
const theme = new URLSearchParams(window.location.search).get("theme");
document.body.dataset.theme = theme;
document.getElementById("theme").textContent = theme;
</script>
</body>
</html>
  • color: #242424 is a dark grey for the light theme, and the rule below it makes the text white when the body has data-theme="dark" or data-theme="contrast".
  • new URLSearchParams(window.location.search).get("theme") reads theme from the page's address.
  • document.body.dataset.theme = theme sets the body's data-theme attribute, and the page also prints the theme it got, so I could see it.

I switched Teams on the web to Dark, as in How to Switch Microsoft Teams to Dark Theme, sent the agent a message and selected Show the menu.

What I Expected​

The request to say dark, so the page would get data-theme="dark" and white text.

The only description of this field I found in Microsoft's Teams docs is on Create and send dialogs, for the request a message extension gets: value.context.theme is the user's client theme, useful for embedded web view formatting. It must be default, contrast or dark. The SDK's own description of TaskModuleRequestContext.Theme, the property behind request.Context?.Theme, is The user's current theme.

What I Got​

Dark text on Teams' dark dialog, hard to read, and the page said Theme: default:

A Teams dialog from the app Dialog Page Demo titled Lunch menu, with Teams in Dark theme: the app name and title are white, but the page's two lines, Today's lunch: vegetable curry with rice. and Theme: default, are dark grey on the dark dialog and barely readable.

What I Checked​

What the agent got. The agent's output had the theme it read from the request:

Agent output
info: LunchMenu.LunchMenuAgent[0]
task/fetch theme: default

The request's value had default too:

task/fetch value
{
"data": {
"task": "lunch",
"type": "task/fetch"
},
"context": {
"theme": "default"
}
}

So the page did what it was told: the agent passed default, and default gets the dark grey text.

Whether Teams was really in Dark. Teams' own parts of the dialog, its title and the app's name, were white on dark grey, so Teams was showing Dark. I reloaded Teams and opened a dialog from the same app again: the request still said default. With Teams in High contrast, it said default too. Every task/fetch request I got from Teams on the web said default, whatever the theme.

What Teams tells the page itself. The Teams JavaScript library (TeamsJS) gives a page its context with microsoftTeams.app.getContext(), and context.app.theme holds the theme. Microsoft's AppInfo reference describes it as The current UI theme of the host. Possible values: "default", "dark", "contrast" or "glass". In a page that printed it, inside the same kind of dialog, it said dark in Dark and contrast in High contrast.

What the bot dialog docs say. Use dialogs with bots, the page about opening dialogs from a bot, doesn't describe the context in the request's value. I found nothing in Microsoft's docs about when Teams sends a theme other than default.

Why It Happens​

In Teams on the web, the task/fetch request said "theme": "default" in every theme I tried, while TeamsJS, asked from inside the page, gave the theme Teams was really showing. Microsoft's docs don't explain the difference. A page that takes its colors from the request's theme therefore gets the light theme's colors on Teams' dark dialog.

The Fix​

Let the page ask Teams for the theme with TeamsJS, and stop passing it from the agent.

1. Read the theme in the page. Replace the page's script:

wwwroot/lunch.html (script)
<script>
async function start() {
await microsoftTeams.app.initialize();

// The theme Teams is showing now, read from Teams itself.
const context = await microsoftTeams.app.getContext();
document.body.dataset.theme = context.app.theme;
document.getElementById("theme").textContent = context.app.theme;
}

start();
</script>
  • await microsoftTeams.app.initialize() connects the page to Teams. TeamsJS's reference for initialize says it must have completed successfully (as determined by the resolved Promise) before any other library calls are made, so getContext() comes after the await.
  • await microsoftTeams.app.getContext() asks Teams for the page's context, theme included.
  • context.app.theme is default, dark or contrast, and goes on the body and in the text, as before.

The CSS stays as it was. The whole page now:

wwwroot/lunch.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lunch menu</title>
<style>
body { margin: 0; padding: 0 24px; font-family: "Segoe UI", system-ui, sans-serif; font-size: 14px; color: #242424; }
body[data-theme="dark"],
body[data-theme="contrast"] { color: #ffffff; }
</style>
</head>
<body>
<p>Today's lunch: vegetable curry with rice.</p>
<p>Theme: <strong id="theme">...</strong></p>

<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>
async function start() {
await microsoftTeams.app.initialize();

// The theme Teams is showing now, read from Teams itself.
const context = await microsoftTeams.app.getContext();
document.body.dataset.theme = context.app.theme;
document.getElementById("theme").textContent = context.app.theme;
}

start();
</script>
</body>
</html>

2. Open the page without a theme. In the agent, give the dialog the page's plain address:

LunchMenuAgent.cs (part)
// task/fetch: open the page. The page reads the theme from Teams itself.
private Task<TaskModuleResponse> OpenPageAsync(ITeamsTurnContext turnContext, ITurnState turnState, TaskModuleRequest request, CancellationToken cancellationToken)
{
_logger.LogInformation("task/fetch theme: {Theme}", request.Context?.Theme);

return Task.FromResult(new TaskModuleResponse
{
Task = new Response
{
Type = TaskModuleResponseTypes.Continue,
Value = new { title = "Lunch menu", width = 400, height = 230, url = _pageUrl },
},
});
}
  • url = _pageUrl opens lunch.html with nothing added to its address.
  • _logger.LogInformation(...) still logs the request's theme, so the output shows what Teams sent.

QueryHelpers isn't used any more, so remove using Microsoft.AspNetCore.WebUtilities; too. Stop the agent with Ctrl+C and run dotnet run again.

How to Match the Microsoft Teams Theme in a Web Page with TeamsJS builds a page like this from an empty file and shows it in each theme.

After the Fix​

With Teams still in Dark, I selected Show the menu again. The page said Theme: dark, and its text was white:

The same Lunch menu dialog in Dark theme, now with Today's lunch: vegetable curry with rice. and Theme: dark in white, easy to read on the dark dialog.

For the same click, the request still said default:

Agent output
info: LunchMenu.LunchMenuAgent[0]
task/fetch theme: default

The page no longer depends on it.

Common Mistakes​

MistakeWhat happensDo this instead
Passing context.theme from task/fetch to the pageIn Teams on the web, it was default in Dark and High contrast tooRead context.app.theme with TeamsJS in the page
Testing the page only in the light themedefault is right there, so nothing looks wrongSwitch Teams to Dark and open the dialog again
Calling getContext() before initialize() has finishedTeamsJS says initialize() must complete before any other callawait app.initialize(), then call getContext()

Troubleshooting​

SymptomLikely causeFix
The page says default while Teams is in DarkThe page still takes the theme from its address, set from the requestRead the theme with app.getContext() in the page
The page keeps showing ...The script stopped before it wrote the theme, for example because TeamsJS didn't loadOpen the browser console and read the first error
The agent's output still says defaultThat's what Teams sent in the requestNothing to fix: the page doesn't use it any more
The page looks the same after the changeThe agent still serves the old pageRestart the agent

FAQ​

Does the desktop app send the right theme?​

I tried only Teams on the web, so I can't say what the desktop and mobile apps send. The fix doesn't depend on it: the page asks Teams for the theme itself.

Can the agent still read context.theme?​

Yes, as request.Context?.Theme, but in my tests it was default in every theme, so don't base a page's colors on it.

Which theme values can the page get?​

Microsoft's AppInfo reference lists default, dark, contrast and glass. In Teams on the web, the page got default in the light theme, dark in Dark and contrast in High contrast.

Conclusion​

The task/fetch request's context.theme said default in Teams on the web even in Dark and High contrast, so a dialog page styled from it showed dark text on Teams' dark dialog. Let the page read the theme itself with TeamsJS: await microsoftTeams.app.initialize(), then microsoftTeams.app.getContext(), and use context.app.theme.

Additional Resources​