Question

I'm developing a WebPart that will be used with Sharepoint and Teams.

My question is easy, I need to retrieve the the microsoftTeams context to get the username.

How can I do it?. So far, I have something like this, but the method is deprecated.

import * as microsoftTeams from '@microsoft/teams-js';

export default class MyWebPart extends BaseClientSideWebPart <IProps> {

   private teamsContext: microsoftTeams.Context;

   protected onInit(): Promise<any> {
    let retVal: Promise<any> = Promise.resolve();
    if (this.context.microsoftTeams) {
      retVal = new Promise((resolve, reject) => {
      this.context.microsoftTeams.getContext(context => {
         this._teamsContext = context;
         resolve();
      });
   });
   }
    return retVal;
   }
}

Regards

Was it helpful?

Solution

Try using if (this.context.sdks.microsoftTeams) instead of if (this.context.microsoftTeams).

Property this.context.microsoftTeams has been deprecated starting from the v1.10 release and you should be using this.context.sdks.microsoftTeams.

Source: Detecting if web part is in Teams context.

Additional Reference: Get context for your Microsoft Teams tab.

Update from Comments:

You can get the teams context details using code like:

let siteTabTitle: string = '';
let userPrincipalName = '';
let userObjectId = '';

if (this.context.sdks.microsoftTeams) {
  siteTabTitle = "We are in the context of following Team: " + this.context.sdks.microsoftTeams.context.teamName;

  userPrincipalName = this.context.sdks.microsoftTeams.context.userPrincipalName;
  userObjectId = this.context.sdks.microsoftTeams.context.userObjectId;
}

Where,

  • {userPrincipalName}: The User Principal Name of the current user, in the current tenant.
  • {userObjectId}: The Azure AD object ID of the current user, in the current tenant.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top