SharePoint Framework - Build Microsoft Teams Tab

Overview

 
The SPFx release of version 1.8 eases the implementation of Microsoft Teams tabs using SharePoint Framework. This gives the possibility of various scenarios in developing Microsoft Teams tabs using SharePoint Framework.
 

Create SPFx WebPart

 
Open the command prompt. Create a directory for SPFx solution.
  1. md spfx-teams-tab  
Navigate to the above-created directory.
  1. cd spfx-teams-tab  
Run the Yeoman SharePoint Generator to create the solution.
  1. yo @microsoft/sharepoint  
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
 
 
Solution Name: Hit Enter to have a default name (spfx-teams-tab in this case) or type in any other name for your solution.
Selected choice: Hit enter
 
Target for component: Here we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 or 2019 onwards).
Selected choice: SharePoint Online only (latest)
 
Place of files: We may choose to use the same folder or create a subfolder for our solution.
Selected choice: Use the current folder
 
Deployment option: Selecting Y will allow the app to be deployed instantly to all sites and will be accessible everywhere.
Selected choice: Y
 
Permissions to access web APIs: Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
Selected choice: N (solution contains unique permissions)
 
Type of client-side component to create: We can choose to create a client-side web part or an extension.
Selected choice: WebPart
 
Web Part Name: Hit Enter to select the default name or type in any other name.
Selected choice: TeamsTab
 
Web part description: Hit Enter to select the default description or type in any other value.
Selected choice: First Teams Tab
 
Framework to use: Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout)
Selected choice: No JavaScript Framework
 
Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock-down the version of project dependencies by running the below command.
  1. npm shrinkwrap  
In the command prompt, type the below command to open the solution in the code editor of your choice.
  1. code .  

Teams in SPFx Solution

 
The SPFx solution contains additional Teams folder in the solution structure with default configurations. This helps to get started with Teams development.
 
  • [webpartid]_color.png: Default small picture for tab
  • [webpartid]_outline.png: Default large picture for tab

Add support for Microsoft Teams

 
Open web part manifest.json file.
 
To the “supportedHosts” property, add “TeamsTab”.
  1. {  
  2.   "$schema""https://developer.microsoft.com/json-schemas/spfx/client-side-web-part-manifest.schema.json",  
  3.   "id""0172ff63-158d-44b5-aa23-99e72a812c02",  
  4.   "alias""TeamsTabWebPart",  
  5.   "componentType""WebPart",  
  6.   
  7.   // The "*" signifies that the version should be taken from the package.json  
  8.   "version""*",  
  9.   "manifestVersion": 2,  
  10.   
  11.   // If true, the component can only be installed on sites where Custom Script is allowed.  
  12.   // Components that allow authors to embed arbitrary script code should set this to true.  
  13.   // https://support.office.com/en-us/article/Turn-scripting-capabilities-on-or-off-1f2c515f-5d7e-448a-9fd7-835da935584f  
  14.   "requiresCustomScript"false,  
  15.   "supportedHosts": ["SharePointWebPart""TeamsTab"],  
  16.   
  17.   "preconfiguredEntries": [{  
  18.     "groupId""5c03119e-3074-46fd-976b-c60198311f70"// Other  
  19.     "group": { "default""Other" },  
  20.     "title": { "default""TeamsTab" },  
  21.     "description": { "default""First teams tab" },  
  22.     "officeFabricIconFontName""Page",  
  23.     "properties": {  
  24.       "description""TeamsTab"  
  25.     }  
  26.   }]  
  27. }  

Code for Microsoft Teams Context

 
Open the web part file (src\webparts\teamsTab\TeamsTabWebPart.ts).
 
Add the below import.
  1. import * as microsoftTeams from '@microsoft/teams-js';  
In a class, define a variable to store Microsoft Teams context.
  1. private _teamsContext: microsoftTeams.Context;  
Add the onInit() method to set the Microsoft Teams context.
  1. protected onInit(): Promise<any> {  
  2.   let retVal: Promise<any> = Promise.resolve();  
  3.   if (this.context.microsoftTeams) {  
  4.     retVal = new Promise((resolve, reject) => {  
  5.       this.context.microsoftTeams.getContext(context => {  
  6.         this._teamsContext = context;  
  7.         resolve();  
  8.       });  
  9.     });  
  10.   }  
  11.   return retVal;  
  12. }  
Update the render() method.
  1. public render(): void {  
  2.   let title: string = '';  
  3.   let siteTabTitle: string = '';  
  4.   
  5.   if (this._teamsContext) {  
  6.     // We have teams context for the web part  
  7.     title = "Welcome to Teams!";  
  8.     siteTabTitle = "Team: " + this._teamsContext.teamName;  
  9.   }  
  10.   else  
  11.   {  
  12.     // We are rendered in normal SharePoint context  
  13.     title = "Welcome to SharePoint!";  
  14.     siteTabTitle = "SharePoint site: " + this.context.pageContext.web.title;  
  15.   }  
  16.   
  17.   this.domElement.innerHTML = `  
  18.     <div class="${ styles.teamsTab }">  
  19.       <div class="${ styles.container }">  
  20.         <div class="${ styles.row }">  
  21.           <div class="${ styles.column }">  
  22.             <span class="${ styles.title }">${title}</span>  
  23.             <p class="${ styles.description }">${siteTabTitle}</p>  
  24.             <p class="${ styles.description }">Description property value - ${escape(this.properties.description)}</p>  
  25.             <a href="https://aka.ms/spfx" class="${ styles.button }">  
  26.               <span class="${ styles.label }">Learn more</span>  
  27.             </a>  
  28.           </div>  
  29.         </div>  
  30.       </div>  
  31.     </div>`;  
  32. }  

Package and Deploy web part to SharePoint

 
On the command prompt, run the below command to bundle the solution.
  1. gulp bundle --ship  
Run the below command to package the solution.
  1. gulp package-solution --ship  
This will create spfx-teams-tab.sppkg package under the SharePoint/solution folder.
 
Open SharePoint App catalog site.
 
Upload spfx-teams-tab.sppkg to the app catalog.
Check “Make this solution available to all sites in the organization” option.
 
Click Deploy.
 

Test the WebPart in SharePoint 

  1. Open SharePoint site
  2. Add the web part to the page.
  3. The web part shows the SharePoint site context information.
 

Make web part available in Microsoft Teams

 
To make the web part available in Microsoft Teams, we will have to synchronize the solution with teams.
  1. Select the solution in the app catalog.
  2. From the ribbon, click Files.
  3. Select Sync to Teams.
 

Test web part in Microsoft Teams

  • Open Microsoft Teams.
  • Under existing team, select channel (by default, General channel is available under team).
  • Click + to add a new tab.
  • Select your custom tab from the list.
  • Click Install.
  • Click Save.
  • Our web part will appear as a custom tab in Microsoft Teams.
 

Summary

 
SharePoint Framework version 1.8 eases the development of Microsoft Teams tabs using SharePoint Framework. The same web part solution can work on both SharePoint and Microsoft Teams based on the context under which it is running. 


Similar Articles