3

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

2
  • Try using if (this.context.sdks.microsoftTeams) instead of if (this.context.microsoftTeams). Mar 4, 2020 at 11:06
  • Error:(47, 50) TS2339: Property 'getContext' does not exist on type 'IMicrosoftTeams'
    – vcima
    Mar 4, 2020 at 11:50

1 Answer 1

2

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.
3
  • Hi Ganesh! With this change: protected onInit(): Promise<any> { let retVal: Promise<any> = Promise.resolve(); if (this.context.sdks.microsoftTeams) { retVal = new Promise((resolve, reject) => { this.context.sdks.microsoftTeams.getContext(context => { this.teamsContext = context; resolve(); }); }); } return retVal; } .
    – vcima
    Mar 4, 2020 at 11:43
  • I have have the problem in: this.context.sdks.microsoftTeams.getContext(context => { Error:(47, 50) TS2339: Property 'getContext' does not exist on type 'IMicrosoftTeams'
    – vcima
    Mar 4, 2020 at 11:45
  • See updated answer to get the current user details in teams context. Mar 4, 2020 at 13:23

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .