How To Use Microsoft Graph Toolkit With SPFx Webpart

Introduction 

 
The Microsoft Graph Toolkit is a web component library for Microsoft Graph that offers reusable and functional UI components for common experiences that work with any web framework. These components can be used in your modern web application, SharePoint web parts, Microsoft Teams tabs, and more. Components are functional and work automatically with the Microsoft Graph Components and work with any web framework and on all modern browsers. IE 11 is also supported. Learn more about Graph Toolkit here.
 
In this article, we will focus on how to use Office Graph Toolkit (preview) with SPFx web part. To use Graph components with SPFx, we will use SharePoint Provider.
 
Step 1  
 
Create an SPFx web part using 'yo @microsoft/sharepoint' command. You can select the below options while creating SPFx solution. We will use a No-JavaScript framework.
 
How To Use Microsoft Graph Toolkit With SPFx Webpart
 
Step 2
 
Run the 'gulp serve' command to check if the default web part is running fine. The local workbench should open. We will add a web part using the below option
 
How To Use Microsoft Graph Toolkit With SPFx Webpart
 
How To Use Microsoft Graph Toolkit With SPFx Webpart
 
Step 3
 
Next, we want to install Microsoft Graph Toolkit via "npm install". Run the below command at the root of the project from NodeJS Command Prompt.
  1. npm install @microsoft/mgt  
How To Use Microsoft Graph Toolkit With SPFx Webpart
 
Step 5
 
Graph Toolkit requires TypeScript 3+, so let us make sure it is updated.
  1. npm install [email protected]  
How To Use Microsoft Graph Toolkit With SPFx Webpart
 
Step 6
 
Open the project in your editor. I used Visual Studio Code.
 
Step 7
 
Modify the code to try MGT(Microsoft Graph ToolKit component).
 
Open \Spfx-MSGraphToolKit\src\webparts\graphToolKitWp\GraphToolKitWpWebPart.ts
Import the providers at the top of the page.
  1. import {Providers, SharePointProvider} from '@microsoft/mgt';  
Add the onInit method to intialize your provider.
  1. // add the onInit() method if not already there in your web part class  
  2. protected async onInit() {  
  3.     Providers.globalProvider = new SharePointProvider(this.context);  
  4. }  
This will set provider to SharePoint context. 
 
Next, let us modify the render method to use try Graph Controls.
  1. public render(): void {  
  2.   
  3.   this.domElement.innerHTML = `<div className={ styles.mgtDemo }>  
  4.   <div className={ styles.container }>  
  5.     <div className={ styles.row }>  
  6.       <span className={ styles.title }>My Day!</span>  
  7.       <h1> Login Control </h1>  
  8.       <mgt-login></mgt-login>  
  9.       <hr>  
  10.       <h1> Person Control </h1>  
  11.       <mgt-person person-query="me" show-name show-email></mgt-person>  
  12.       <hr>  
  13.       <h1> Agenda Control </h1>  
  14.       <mgt-agenda  group-by-day></mgt-agenda>  
  15.       <hr>  
  16.       <h1> People Control <h1>  
  17.       <mgt-people></mgt-people>  
  18.     </div>  
  19.   </div>  
  20. </div>`;  
  21.   
  22. }  
Here, we are trying 4 controls. For more details on configurable options, you can refer to the respective links.
  • Login - This displays either sign in button or user's name if already user is already logged in (like in welcome control)
  • Person - This control can display any person's details, we can use person-query to configure which user's details is to be shown
  • Agenda - This control will display all calendar events of the logged-in user.
  • People - This will display a group of people or contacts by using their photos or initial.
Step 8
 
Provide Azure AD App Permission to access Graph API.
 
As the Graph Toolkit is using GRAPH API internally, we need to provide API permission to 'SharePoint Online Client Extensibility Web Application Principal'. 
  • Login to Portal.Azure.com
  • Go to Azure AD from left navigation
  • Select App Registration from the blade.
  • Select 'SharePoint Online Client Extensibility Web Application Principal'
How To Use Microsoft Graph Toolkit With SPFx Webpart
  • Select API Permission.
  • Add below 4 permissions (marked as a green arrow). Please ignore other ones. This is not required for this example.
How To Use Microsoft Graph Toolkit With SPFx Webpart
 
Step 9
 
Try WebPart of SharePoint workbench 
  • run 'gulp serve'
  • Once it is running, open SharePoint workbench.

    https://jungle.sharepoint.com/sites/simba/_layouts/15/workbench.aspx  
  • Add 'GraphToolKitWP' to Page
  • Wait for some time and you will see the below output according to your tenant data. 
How To Use Microsoft Graph Toolkit With SPFx Webpart
 
One final note - For deploying this to the app catalog, you need to modify package-solution.json for web API Permission request. Below is the JSON file.
  1. {  
  2.   "$schema""https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",  
  3.   "solution": {  
  4.     "name""spfx-ms-graph-tool-kit-client-side-solution",  
  5.     "id""10c9f59c-0e2d-44ae-8f2e-9de9c5c507d6",  
  6.     "version""1.0.0.0",  
  7.     "includeClientSideAssets"true,  
  8.     "isDomainIsolated"false,  
  9.     "webApiPermissionRequests": [  
  10.       {  
  11.         "resource""Microsoft Graph",  
  12.         "scope""User.Read"  
  13.       },  
  14.       {  
  15.         "resource""Microsoft Graph",  
  16.         "scope""User.ReadBasic.All"  
  17.       },  
  18.       {  
  19.         "resource""Microsoft Graph",  
  20.         "scope""People.Read"  
  21.       },  
  22.       {  
  23.         "resource""Microsoft Graph",  
  24.         "scope""Calendars.Read"  
  25.       }  
  26.     ]  
  27.   },  
  28.   "paths": {  
  29.     "zippedPackage""solution/spfx-ms-graph-tool-kit.sppkg"  
  30.   }  
  31. }  
That's it. We have just tested the Graph Tool Kit on SharePoint workbench. As of the date of writing this article, please note some important points related to Graph Tool Kit,
  • It is still in preview mode and not recommended for prod use.
  • Packaging this web part is giving some errors/warnings(refer this logged issue)
  • You can follow Graph Tool Kit official documentation for options available for each control

Conclusion

 
In this article, we have learned how to use Graph ToolKit within SPFx web part. This toolkit would be very useful for many business use cases. Now, we don't have to separately call Graph API and use our custom HTML to display Office 365 data. Though as of now we have limited controls, we can expect others very soon.