How to start a new SPFx web part project with Microsoft Graph Toolkit and React

Published 03-07-2021 07:46 AM 2,130 Views
Senior Member

Context

Microsoft Graph Toolkit is a set of reusable components working with Microsoft Graph to accelerate your web developments with SharePoint Online, Microsoft Teams and more. With this kit, developers don’t have to write any code for authentification and authorization so I’m sure you’ll save a lot of time using it in your various projects. Now let’s see how we setup a SPFx web part project with Microsoft Graph Toolkit and React!

 

Create a new SPFx web part project

First, we need to create a new SPFx web part project with React framework using Yeoman generator tool:

 

yo @microsoft/sharepoint

 

Note: don’t forget to choose React framework during the setup of your project and use default project settings.

 

Install Microsoft Graph Toolkit for React

To install Microsoft Graph Toolkit for React, we need to install two packages with npm as below:

  1. mgt
  2. mgt-react

But why two packages? Because we need to use Providers and SharePointProvider classes included in mgt to pass the web part context to mgt-react components inside onInit method of the web part.

Install the two required packages with the following npm commands:

 

npm i @microsoft/mgt
npm i @microsoft/mgt-react

 

Install MGT dependencies

To use Microsoft Graph Toolkit components with React, we need to use TypeScript 3.7 at least. Install this dependency with the following npm command:

 

npm i @microsoft/rush-stack-compiler-3.7 --save-dev

 

Note: -dev command add this package inside the devDependencies section in package.json file.

 

Then we need to update the tsconfig.json with the following content at line 2:

 

"extends": "./node_modules/@microsoft/rush-stack-compiler-3.7/includes/tsconfig-web.json"

 

Finally remove the following line from tslint.json to compile the project without any warning:

 

"no-use-before-declare": true

 

Configure MGT in the project

Now we have installed all the required dependencies, we can configure MGT in our project. First you need to add the API permissions in your package-solution.json file as below:

 

    "webApiPermissionRequests": [
      {
          "resource": "Microsoft Graph",
          "scope": "User.Read.All"
      }
    ]

 

Note: you need to add all API permissions depending on which MGT components you are using in your project. In this example, I just need to read user profiles to display some properties in the web part with a Person component.

 

After this, we need to pass the context of the web part to MGT components, add the following code in your web part file (in my project MgtReactDemoWebPart) as below:

 

import { Providers, SharePointProvider } from '@microsoft/mgt';

// ...

protected onInit() {
  Providers.globalProvider = new SharePointProvider(this.context);
  return super.onInit();
}

 

Use MGT components in the project

We are ready to use MGT components in our project, in this example I’ll use the Person component to display basic informations about the current user. To do this, we need to import classes from mgt-react then using them in our render method as below:

 

import { Person, PersonViewType } from '@microsoft/mgt-react';

// ...

public render(): React.ReactElement<IMgtReactDemoProps> {
    return (
      <div className={ styles.mgtReactDemo }>
        <Person personQuery="me" view={PersonViewType.twolines} line2Property={"jobTitle"}></Person>
      </div>
    );
}

 

We can now build and run our project for SharePoint Online service (no local workbench) using the following command:

 

gulp serve --nobrowser

 

To debug your web part in SharePoint Online service, use the workbench located here:

 

https://your-sharepoint-online-site/_layouts/workbench.aspx

 

We can now see the result of our web part using MGT in the SPO workbench:

mgt-react-result-2.png

That’s it, you have configured a SPFx web part project with Microsoft Graph Toolkit and React. You can now integrate other components in your web part project without writing any line of code. And don’t forget to look at the Get component which provides a great way to query any Microsoft Graph endpoint and display the results with a custom template you made for your line of business application ;)

 

You can read this article in my blog.

 

Resources

https://docs.microsoft.com/en-us/graph/toolkit/get-started/overview

https://docs.microsoft.com/en-us/graph/toolkit/get-started/mgt-react

https://www.npmjs.com/package/@microsoft/mgt

https://www.npmjs.com/package/@microsoft/mgt-react

%3CLINGO-SUB%20id%3D%22lingo-sub-2191526%22%20slang%3D%22en-US%22%3EHow%20to%20start%20a%20new%20SPFx%20web%20part%20project%20with%20Microsoft%20Graph%20Toolkit%20and%20React%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2191526%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EContext%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EMicrosoft%20Graph%20Toolkit%20is%20a%20set%20of%20reusable%20components%20working%20with%20Microsoft%20Graph%20to%20accelerate%20your%20web%20developments%20with%20SharePoint%20Online%2C%20Microsoft%20Teams%20and%20more.%20With%20this%20kit%2C%20developers%20don%E2%80%99t%20have%20to%20write%20any%20code%20for%20authentification%20and%20authorization%20so%20I%E2%80%99m%20sure%20you%E2%80%99ll%20save%20a%20lot%20of%20time%20using%20it%20in%20your%20various%20projects.%20Now%20let%E2%80%99s%20see%20how%20we%20setup%20a%20SPFx%20web%20part%20project%20with%20Microsoft%20Graph%20Toolkit%20and%20React!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ECreate%20a%20new%20SPFx%20web%20part%20project%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EFirst%2C%20we%20need%20to%20create%20a%20new%20SPFx%20web%20part%20project%20with%20React%20framework%20using%20Yeoman%20generator%20tool%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eyo%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F41501%22%20target%3D%22_blank%22%3E%40microsoft%3C%2FA%3E%2Fsharepoint%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20don%E2%80%99t%20forget%20to%20choose%20React%20framework%20during%20the%20setup%20of%20your%20project%20and%20use%20default%20project%20settings.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EInstall%20Microsoft%20Graph%20Toolkit%20for%20React%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3C!--%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wp%3Aparagraph%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20--%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3ETo%20install%20Microsoft%20Graph%20Toolkit%20for%20React%2C%20we%20need%20to%20install%20two%20packages%20with%20npm%20as%20below%3A%3C%2FP%3E%3COL%3E%3CLI%3Emgt%3C%2FLI%3E%3CLI%3Emgt-react%3C%2FLI%3E%3C%2FOL%3E%3CP%3EBut%20why%20two%20packages%3F%20Because%20we%20need%20to%20use%20%3CSTRONG%3EProviders%3C%2FSTRONG%3E%20and%20%3CSTRONG%3ESharePointProvider%3C%2FSTRONG%3E%20classes%20included%20in%20mgt%20to%20pass%20the%20web%20part%20context%20to%20mgt-react%20components%20inside%20onInit%20method%20of%20the%20web%20part.%3C%2FP%3E%3CP%3EInstall%20the%20two%20required%20packages%20with%20the%20following%20npm%20commands%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Enpm%20i%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F41501%22%20target%3D%22_blank%22%3E%40microsoft%3C%2FA%3E%2Fmgt%0Anpm%20i%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F41501%22%20target%3D%22_blank%22%3E%40microsoft%3C%2FA%3E%2Fmgt-react%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3C!--%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2Fwp%3Aparagraph%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20--%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EInstall%20MGT%20dependencies%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3ETo%20use%20Microsoft%20Graph%20Toolkit%20components%20with%20React%2C%20we%20need%20to%20use%20%3CSTRONG%3ETypeScript%203.7%3C%2FSTRONG%3E%20at%20least.%20Install%20this%20dependency%20with%20the%20following%20npm%20command%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Enpm%20i%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F41501%22%20target%3D%22_blank%22%3E%40microsoft%3C%2FA%3E%2Frush-stack-compiler-3.7%20--save-dev%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C!--%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wp%3Aparagraph%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20--%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20-dev%20command%20add%20this%20package%20inside%20the%20%3CSTRONG%3EdevDependencies%3C%2FSTRONG%3E%20section%20in%20%3CSTRONG%3Epackage.json%3C%2FSTRONG%3E%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20we%20need%20to%20update%20the%20%3CSTRONG%3Etsconfig.json%3C%2FSTRONG%3E%20with%20the%20following%20content%20at%20line%202%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%22extends%22%3A%20%22.%2Fnode_modules%2F%40microsoft%2Frush-stack-compiler-3.7%2Fincludes%2Ftsconfig-web.json%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFinally%20remove%20the%20following%20line%20from%20%3CSTRONG%3Etslint.json%3C%2FSTRONG%3E%20to%20compile%20the%20project%20without%20any%20warning%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%22no-use-before-declare%22%3A%20true%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EConfigure%20MGT%20in%20the%20project%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3ENow%20we%20have%20installed%20all%20the%20required%20dependencies%2C%20we%20can%20configure%20MGT%20in%20our%20project.%20First%20you%20need%20to%20add%20the%20API%20permissions%20in%20your%20%3CSTRONG%3Epackage-solution.json%3C%2FSTRONG%3E%20file%20as%20below%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%20%20%20%20%22webApiPermissionRequests%22%3A%20%5B%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22resource%22%3A%20%22Microsoft%20Graph%22%2C%0A%20%20%20%20%20%20%20%20%20%20%22scope%22%3A%20%22User.Read.All%22%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%5D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C!--%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wp%3Aparagraph%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20--%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20you%20need%20to%20add%20all%20API%20permissions%20depending%20on%20which%20MGT%20components%20you%20are%20using%20in%20your%20project.%20In%20this%20example%2C%20I%20just%20need%20to%20read%20user%20profiles%20to%20display%20some%20properties%20in%20the%20web%20part%20with%20a%20Person%20component.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20this%2C%20we%20need%20to%20pass%20the%20context%20of%20the%20web%20part%20to%20MGT%20components%2C%20add%20the%20following%20code%20in%20your%20web%20part%20file%20(in%20my%20project%20MgtReactDemoWebPart)%20as%20below%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eimport%20%7B%20Providers%2C%20SharePointProvider%20%7D%20from%20'%40microsoft%2Fmgt'%3B%0A%0A%2F%2F%20...%0A%0Aprotected%20onInit()%20%7B%0A%20%20Providers.globalProvider%20%3D%20new%20SharePointProvider(this.context)%3B%0A%20%20return%20super.onInit()%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C!--%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2Fwp%3Aparagraph%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20--%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EUse%20MGT%20components%20in%20the%20project%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EWe%20are%20ready%20to%20use%20MGT%20components%20in%20our%20project%2C%20in%20this%20example%20I%E2%80%99ll%20use%20the%20Person%20component%20to%20display%20basic%20informations%20about%20the%20current%20user.%20To%20do%20this%2C%20we%20need%20to%20import%20classes%20from%20mgt-react%20then%20using%20them%20in%20our%20render%20method%20as%20below%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Eimport%20%7B%20Person%2C%20PersonViewType%20%7D%20from%20'%40microsoft%2Fmgt-react'%3B%0A%0A%2F%2F%20...%0A%0Apublic%20render()%3A%20React.ReactElement%3CIMGTREACTDEMOPROPS%3E%20%7B%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%3CDIV%20classname%3D%22%7B%22%20styles.mgtreactdemo%3D%22%22%3E%0A%20%20%20%20%20%20%20%20%3CPERSON%20personquery%3D%22me%22%20view%3D%22%7BPersonViewType.twolines%7D%22%20line2property%3D%22%7B%26quot%3BjobTitle%26quot%3B%7D%22%3E%3C%2FPERSON%3E%0A%20%20%20%20%20%20%3C%2FDIV%3E%0A%20%20%20%20)%3B%0A%7D%3C%2FIMGTREACTDEMOPROPS%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20can%20now%20build%20and%20run%20our%20project%20for%20SharePoint%20Online%20service%20(no%20local%20workbench)%20using%20the%20following%20command%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Egulp%20serve%20--nobrowser%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20debug%20your%20web%20part%20in%20SharePoint%20Online%20service%2C%20use%20the%20workbench%20located%20here%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Ehttps%3A%2F%2Fyour-sharepoint-online-site%2F_layouts%2Fworkbench.aspx%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20can%20now%20see%20the%20result%20of%20our%20web%20part%20using%20MGT%20in%20the%20SPO%20workbench%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22mgt-react-result-2.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F261463i1C709E416F956B22%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22mgt-react-result-2.png%22%20alt%3D%22mgt-react-result-2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EThat%E2%80%99s%20it%2C%20you%20have%20configured%20a%20SPFx%20web%20part%20project%20with%20Microsoft%20Graph%20Toolkit%20and%20React.%20You%20can%20now%20integrate%20other%20components%20in%20your%20web%20part%20project%20without%20writing%20any%20line%20of%20code.%20And%20don%E2%80%99t%20forget%20to%20look%20at%20the%20%3CSTRONG%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Ftoolkit%2Fcomponents%2Fget%22%20target%3D%22_blank%22%20rel%3D%22noreferrer%20noopener%22%3EGet%3C%2FA%3E%3C%2FSTRONG%3E%20component%20which%20provides%20a%20great%20way%20to%20query%20any%20Microsoft%20Graph%20endpoint%20and%20display%20the%20results%20with%20a%20custom%20template%20you%20made%20for%20your%20line%20of%20business%20application%20%3B)%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20read%20this%20article%20in%20my%20%3CA%20title%3D%22blog%22%20href%3D%22https%3A%2F%2Fyhabersaat.ch%2F2021%2F03%2F06%2Fspfx-web-part-microsoft-graph-toolkit-react%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Eblog%3C%2FA%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EResources%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3C!--%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wp%3Aparagraph%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20--%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Ftoolkit%2Fget-started%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noreferrer%20noopener%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Ftoolkit%2Fget-started%2Foverview%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Ftoolkit%2Fget-started%2Fmgt-react%22%20target%3D%22_blank%22%20rel%3D%22noreferrer%20noopener%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Ftoolkit%2Fget-started%2Fmgt-react%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40microsoft%2Fmgt%22%20target%3D%22_blank%22%20rel%3D%22noreferrer%20noopener%20nofollow%22%3Ehttps%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40microsoft%2Fmgt%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40microsoft%2Fmgt-react%22%20target%3D%22_blank%22%20rel%3D%22noreferrer%20noopener%20nofollow%22%3Ehttps%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40microsoft%2Fmgt-react%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2191526%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EMicrosoft%20Graph%20Toolkit%20is%20a%20set%20of%20reusable%20components%20working%20with%20Microsoft%20Graph%20to%20accelerate%20your%20web%20developments%20with%20SharePoint%20Online%2C%20Microsoft%20Teams%20and%20more.%20With%20this%20kit%2C%20developers%20don%E2%80%99t%20have%20to%20write%20any%20code%20for%20authentification%20and%20authorization%20so%20I%E2%80%99m%20sure%20you%E2%80%99ll%20save%20a%20lot%20of%20time%20using%20it%20in%20your%20various%20projects.%20Now%20let%E2%80%99s%20see%20how%20we%20setup%20a%20SPFx%20web%20part%20project%20with%20Microsoft%20Graph%20Toolkit%20and%20React!%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Co-Authors
Version history
Last update:
‎Mar 08 2021 12:38 AM
Updated by: