MicrosoftDocs / msteams-docs

Source for the Microsoft Teams developer platform documentation.
https://aka.ms/teamsdev
Creative Commons Attribution 4.0 International
281 stars 502 forks source link

Teams-only Apps code snippet needs update #10597

Closed erinha closed 5 months ago

erinha commented 5 months ago

Type of issue

Typo

Feedback

A code snippet is provided for app developers to check if their app is running in Teams. https://learn.microsoft.com/en-us/microsoftteams/platform/tabs/how-to/using-teams-client-library?tabs=javascript%2Cmanifest-teams-toolkit#teams-only-apps

This snippet needs to be updated to include checking for app host name with value microsoftTeams.HostName.teamsModern.

BEFORE UPDATE (i.e., current code on doc page):

// Ensure that you initialize the TeamsJS once, regardless of how often this is called.
let teamsInitPromise;
export function ensureTeamsJSInitialized(){
    if (!teamsInitPromise) {
        teamsInitPromise = microsoftTeams.app.initialize();
    }
    return teamsInitPromise;
}

// Function returns a promise which resolves to true if we're running in Teams
export async function inTeams(){
  try {
    await ensureTeamsJSInitialized();
    const context = await microsoftTeams.app.getContext();
    return (context.app.host.name === microsoftTeams.HostName.teams);
  }
  catch (e) {
    console.log(`${e} from Teams SDK, may be running outside of Teams`);
    return false;
  }
}

AFTER SUGGESTED FIX:

// Ensure that you initialize the TeamsJS once, regardless of how often this is called.
let teamsInitPromise;
export function ensureTeamsJSInitialized(){
    if (!teamsInitPromise) {
        teamsInitPromise = microsoftTeams.app.initialize();
    }
    return teamsInitPromise;
}

// Function returns a promise which resolves to true if we're running in Teams
export async function inTeams(){
  try {
    await ensureTeamsJSInitialized();
    const context = await microsoftTeams.app.getContext();
    return (context.app.host.name === microsoftTeams.HostName.teams || context.app.host.name == microsoftTeams.HostName.teamsModern);
  }
  catch (e) {
    console.log(`${e} from Teams SDK, may be running outside of Teams`);
    return false;
  }
}

Page URL

https://learn.microsoft.com/en-us/microsoftteams/platform/tabs/how-to/using-teams-client-library?tabs=javascript%2Cmanifest-teams-toolkit#teams-only-apps

Content source URL

https://github.com/MicrosoftDocs/msteams-docs/blob/main/msteams-platform/tabs/how-to/using-teams-client-library.md

Author

@erikadoyle

Document Id

4ae8187e-a7e4-4b5a-6f38-da6bfa30f2d2

microsoft-github-policy-service[bot] commented 5 months ago

Hi erinha! Thank you for bringing this issue to our attention. We will investigate and if we require further information we will reach out in one business day. Please use this link to escalate if you don't get replies.

Best regards, Teams Platform

erikadoyle commented 5 months ago

Thanks @erinha! The consensus across Teams/Taos PM was to remove the snippet entirely. Update is staged and ready for merge: https://github.com/MicrosoftDocs/msteams-docs/pull/10588

microsoft-github-policy-service[bot] commented 5 months ago

Tell us about your experience!

Hi erinha! This issue is closed in our system. We would like your feedback on your experience with our support team and Platform.

Best regards, Teams Platform