" MicromOne: Detecting Microsoft Teams Context in Dynamics 365 Using JavaScript

Pagine

Detecting Microsoft Teams Context in Dynamics 365 Using JavaScript

When working with Dynamics 365 or Power Apps embedded inside Microsoft Teams, developers often need to understand where the application is actually running.

Is it opened directly in a browser?
Is it hosted inside Microsoft Teams?
Is it loaded inside an iframe?

This article explains why window.location.href can be misleading in Teams integrations and how to detect the execution context reliably using JavaScript.

The Problem: URL Confusion in Microsoft Teams

When a Dynamics 365 app is opened inside Microsoft Teams, it is wrapped in multiple layers:

  • Microsoft Teams shell

  • Power Apps web player

  • Dynamics 365 Unified Interface (UCI)

  • Your page, usually inside an iframe

Because of this architecture, checking the URL with JavaScript like this:

const url = window.location.href;
console.log(url);

can return very different results depending on where the script runs.

Common URL Results You May Encounter

When Dynamics 365 is embedded in Teams, the URL often looks like this:

https://org.crm4.dynamics.com/uclient/main.htm?...&source=teamstab

When Dynamics 365 is opened directly in a browser, the URL may look like:

https://org.crm4.dynamics.com/main.aspx?appid=...

If the code is executed at the Teams shell level, the URL may simply be:

https://teams.microsoft.com/v2

This inconsistency makes it difficult to determine whether the app is actually running inside Microsoft Teams.

Using parent.window.location.href

If your JavaScript runs inside an iframe, you might try accessing the parent window:

const url = parent.window.location.href;
console.log(url);

In Microsoft Teams, this often still resolves to:

https://teams.microsoft.com/v2

This behavior is expected because Teams heavily sandboxes embedded content and hides the real navigation context for security reasons.

The Key Indicator: source=teamstab

In Dynamics 365, one of the most reliable indicators that the app is opened inside Microsoft Teams is the presence of the query parameter:

source=teamstab

If this parameter exists in the URL, the app was launched from a Microsoft Teams tab.

Example:

https://org.crm4.dynamics.com/uclient/main.htm?...&source=teamstab

Practical Detection Example

The following JavaScript example shows a defensive approach to detect whether the app is running outside Microsoft Teams:

const url = new URL(parent.window.location.href);

if (
  !url.toString().includes("source=teams") &&
  !url.toString().includes("teamstab")
) {
  console.log("Running outside Microsoft Teams");
} else {
  console.log("Running inside Microsoft Teams");
}

This logic works well for Dynamics 365 JavaScript customizations, web resources, embedded Power Apps, and model-driven app extensions.

Best Practices and Considerations

Avoid relying only on window.location, because in embedded scenarios it often reflects only the iframe URL.

Expect strong isolation when running inside Teams. Access to parent and top window objects may be restricted or normalized.

When available, use official APIs such as the Microsoft Teams JavaScript SDK or Power Apps context APIs. These provide explicit context information instead of relying on URL parsing.