In this article, you will see how to list joined Teams using Graph API in SharePoint Framework by performing the following tasks,

List joined Teams

Get the teams in Microsoft Teamsof which the user is a direct member.
Note
Each team redirects to the respective team.
List Joined Teams Using Graph API In SharePoint Framework
Prerequisites

Create SPFx solution

Open Node.js command prompt.
Create a new folder.
>md spfx-list-joinedteams
Navigate to the folder.
>cd spfx-list-joinedteams
Execute the following command to create SPFx webpart.
>yo @microsoft/sharepoint
Enter all the required details to create a new solution as shown below.
List Joined Teams Using Graph API In SharePoint Framework
Yeoman generator will perform the scaffolding process and once it is completed, lock down the version of project dependencies by executing the following command.
>npm shrinkwrap
Execute the following command to open the solution in the code editor.
>code .

Implement SPFx solution

Execute the following command to install Microsoft Graph Typings.
> npm install @microsoft/microsoft-graph-types
Folder Structure
List Joined Teams Using Graph API In SharePoint Framework
Open package-solution.json “config\package-solution.json” file and update the code as shown below.
  1. {
  2. "$schema": "https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
  3. "solution": {
  4. "name": "spfx-list-joinedteams-client-side-solution",
  5. "id": "2a4704fa-f387-4680-9b54-fe2003b78844",
  6. "version": "1.0.0.0",
  7. "includeClientSideAssets": true,
  8. "isDomainIsolated": false,
  9. "webApiPermissionRequests": [
  10. {
  11. "resource": "Microsoft Graph",
  12. "scope": "User.Read.All"
  13. },
  14. {
  15. "resource": "Microsoft Graph",
  16. "scope": "User.ReadWrite.All"
  17. }
  18. ]
  19. },
  20. "paths": {
  21. "zippedPackage": "solution/spfx-list-joinedteams.sppkg"
  22. }
  23. }
Create a new folder named as “models” inside src\webparts\listJoinedteams folder. Create a new file named as ITeam.ts. Open “src\webparts\listJoinedteams\models\ITeam.ts” file and update the code as shown below.
  1. import { Guid } from '@microsoft/sp-core-library';
  2. export interface ITeam {
  3. teamId: Guid;
  4. displayName: string;
  5. webUrl?: string;
  6. }
  7. export interface ITeamCollection {
  8. value: ITeam[];
  9. }
Create a new file named as IListJoinedteamsState.ts under components folder. Open “src\webparts\listJoinedteams\components\IListJoinedteamsState.ts” file and update the code as shown below.
  1. import { ITeam } from '../models/ITeam';
  2. export interface IListJoinedteamsState {
  3. joinedTeams?: ITeam[];
  4. }
Create a new folder named as “services” inside src\webparts\listJoinedteams folder. Create a new file named as ListJoinedTeamsService.ts. Open “src\webparts\listJoinedteams\services\ListJoinedTeamsService.ts” file and update the code as shown below.
  1. import { MSGraphClient } from "@microsoft/sp-http";
  2. import { WebPartContext } from "@microsoft/sp-webpart-base";
  3. import { ITeam, ITeamCollection } from '../models/ITeam';
  4. export class ListJoinedTeamsService {
  5. public context: WebPartContext;
  6. public setUp(context: WebPartContext): void {
  7. this.context = context;
  8. }
  9. public getJoinedTeams(): Promise<ITeam[]> {
  10. return new Promise<ITeam[]>((resolve, reject) => {
  11. try {
  12. // Prepare the output array
  13. var teams: Array<ITeam> = new Array<ITeam>();
  14. this.context.msGraphClientFactory
  15. .getClient()
  16. .then((client: MSGraphClient) => {
  17. client
  18. .api("/me/joinedTeams")
  19. .select('id,displayName')
  20. .get((error: any, teamColl: ITeamCollection, rawResponse: any) => {
  21. // Map the response to the output array
  22. teamColl.value.map((item: any) => {
  23. teams.push({
  24. teamId: item.id,
  25. displayName: item.displayName
  26. });
  27. });
  28. resolve(teams);
  29. });
  30. });
  31. } catch (error) {
  32. console.error(error);
  33. }
  34. });
  35. }
  36. public getTeamWebUrl(teams: ITeam): Promise<any> {
  37. return new Promise<any>((resolve, reject) => {
  38. try {
  39. this.context.msGraphClientFactory
  40. .getClient()
  41. .then((client: MSGraphClient) => {
  42. client
  43. .api(`/teams/${teams.teamId}`)
  44. .select('webUrl')
  45. .get((error: any, team: any, rawResponse: any) => {
  46. resolve(team);
  47. });
  48. });
  49. } catch (error) {
  50. console.error(error);
  51. }
  52. });
  53. }
  54. }
  55. const listJoinedTeamsService = new ListJoinedTeamsService();
  56. export default listJoinedTeamsService;
Open “src\webparts\listJoinedteams\ListJoinedteamsWebPart.ts” file and update the following.
Import modules,
  1. import listJoinedTeamsService, {ListJoinedTeamsService} from './services/ListJoinedTeamsService';
Update the OnInit method,
  1. protected onInit():Promise<void>{
  2. return super.onInit().then(() => {
  3. listJoinedTeamsService.setUp(this.context);
  4. });
  5. }
Update the React component (src\webparts\listJoinedteams\components\ListJoinedteams.tsx),
  1. import * as React from 'react';
  2. import styles from './ListJoinedteams.module.scss';
  3. import { IListJoinedteamsProps } from './IListJoinedteamsProps';
  4. import { IListJoinedteamsState } from './IListJoinedteamsState';
  5. import { escape } from '@microsoft/sp-lodash-subset';
  6. import { ITeam, ITeamCollection } from '../models/ITeam';
  7. import listJoinedTeamsService, { ListJoinedTeamsService } from '../services/ListJoinedTeamsService';
  8. import { List } from 'office-ui-fabric-react/lib/List';
  9. import { FocusZone, FocusZoneDirection } from 'office-ui-fabric-react/lib/FocusZone';
  10. import { TextField } from 'office-ui-fabric-react/lib/TextField';
  11. import { ITheme, mergeStyleSets, getTheme, getFocusStyle } from 'office-ui-fabric-react/lib/Styling';
  12. interface IGroupListClassObject {
  13. itemCell: string;
  14. itemImage: string;
  15. itemContent: string;
  16. itemName: string;
  17. itemIndex: string;
  18. chevron: string;
  19. }
  20. const theme: ITheme = getTheme();
  21. const { palette, semanticColors, fonts } = theme;
  22. const classNames: IGroupListClassObject = mergeStyleSets({
  23. itemCell: [
  24. getFocusStyle(theme, { inset: -1 }),
  25. {
  26. minHeight: 54,
  27. padding: 10,
  28. boxSizing: 'border-box',
  29. borderBottom: `1px solid ${semanticColors.bodyDivider}`,
  30. display: 'flex',
  31. selectors: {
  32. '&:hover': { background: palette.neutralLight }
  33. }
  34. }
  35. ],
  36. itemImage: {
  37. flexShrink: 0
  38. },
  39. itemContent: {
  40. marginLeft: 10,
  41. overflow: 'hidden',
  42. flexGrow: 1
  43. },
  44. itemName: [
  45. fonts.xLarge,
  46. {
  47. whiteSpace: 'nowrap',
  48. overflow: 'hidden',
  49. textOverflow: 'ellipsis'
  50. }
  51. ],
  52. itemIndex: {
  53. fontSize: fonts.small.fontSize,
  54. color: palette.neutralTertiary,
  55. marginBottom: 10
  56. },
  57. chevron: {
  58. alignSelf: 'center',
  59. marginLeft: 10,
  60. color: palette.neutralTertiary,
  61. fontSize: fonts.large.fontSize,
  62. flexShrink: 0
  63. }
  64. });
  65. export default class ListJoinedteams extends React.Component<IListJoinedteamsProps, IListJoinedteamsState> {
  66. private _originalItems: ITeam[] = [];
  67. constructor(props: IListJoinedteamsProps) {
  68. super(props);
  69. this.state = {
  70. joinedTeams: []
  71. };
  72. }
  73. public componentDidMount(): void {
  74. this._getJoinedTeams();
  75. }
  76. public render(): React.ReactElement<IListJoinedteamsProps> {
  77. const { joinedTeams = [] } = this.state;
  78. return (
  79. <FocusZone direction={FocusZoneDirection.vertical}>
  80. <h1>My Teams</h1>
  81. <List items={joinedTeams} onRenderCell={this._onRenderCell} />
  82. </FocusZone>
  83. );
  84. }
  85. public _getJoinedTeams = (): void => {
  86. listJoinedTeamsService.getJoinedTeams().then(result => {
  87. this.setState({
  88. joinedTeams: result
  89. });
  90. this._getTeamWebUrl(result);
  91. });
  92. }
  93. public _getTeamWebUrl = (teams: any): void => {
  94. teams.map(teamItem => (
  95. listJoinedTeamsService.getTeamWebUrl(teamItem).then(teamUrl => {
  96. if (teamUrl !== null) {
  97. this.setState(prevState => ({
  98. joinedTeams: prevState.joinedTeams.map(team => team.teamId === teamItem.teamId ? { ...team, webUrl: teamUrl.webUrl } : team)
  99. }));
  100. }
  101. })
  102. ));
  103. }
  104. private _onRenderCell(team: ITeam, index: number | undefined): JSX.Element {
  105. return (
  106. <div className={classNames.itemCell} data-is-focusable={true}>
  107. <div className={classNames.itemContent}>
  108. <div className={classNames.itemName}>
  109. <a href={team.webUrl} target="_blank">{team.displayName}</a>
  110. </div>
  111. </div>
  112. </div>
  113. );
  114. }
  115. }

Deploy the solution

Execute the following commands to bundle and package the solution.
>gulp bundle --ship
>gulp package-solution --ship
Navigate to tenant app catalog – Example: https://c986.sharepoint.com/sites/appcatalog/SitePages/Home.aspx
Upload the package file (sharepoint\solution\spfx-list-joinedteams.sppkg). Click Deploy.
List Joined Teams Using Graph API In SharePoint Framework

Approve Graph API permission

Navigate to SharePoint Admin center, click API access in the left navigation. Select the permission requested and click Approve.
List Joined Teams Using Graph API In SharePoint Framework
List Joined Teams Using Graph API In SharePoint Framework

Test the webpart

Navigate to the SharePoint site and add the app.
List Joined Teams Using Graph API In SharePoint Framework
Navigate to the page and add the webpart.
List Joined Teams Using Graph API In SharePoint Framework

Summary

Thus, in this article, you saw how to list joined Teams using Graph API in SharePoint Framework.