Events
Microsoft 365 Community Conference
May 6, 2 PM - May 9, 12 AM
Skill up for the era of AI at the ultimate community-led Microsoft 365 event, May 6-8 in Las Vegas.
Learn moreThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
When building SharePoint Framework solutions, the easiest way to show data from Microsoft 365 is by using Microsoft Graph Toolkit.
Microsoft Graph Toolkit is a collection of reusable, framework-agnostic components and authentication providers for accessing and working with Microsoft Graph. The components are fully functional right out of the box, with built-in providers that authenticate with and fetch data from Microsoft Graph.
Microsoft Graph Toolkit makes it easy to use Microsoft Graph in your application. The following example shows the calendar events of the signed-in user with just one line of code using the Agenda component.
When building solutions using SharePoint Framework, you connect to Microsoft Graph using the MSGraphClient exposed by SharePoint Framework. The MSGraphClient handles authentication with Microsoft Graph for you, allowing you to focus on building your app. Still, you need to build the necessary API requests, execute them, handle their responses, and show the data in your app.
Microsoft Graph Toolkit simplifies working with Microsoft Graph even further. Not only does it handle the authentication, but it also communicates with Microsoft Graph for you and automatically shows the retrieved data in your app, saving you precious time.
The following example shows how to use the toolkit's Agenda component in a plain JavaScript SharePoint Framework web part to display the current user's calendar events:
import { Providers, SharePointProvider } from '@microsoft/mgt-spfx';
// ...
export default class CalendarWebPart extends BaseClientSideWebPart<ICalendarWebPartProps> {
protected async onInit() {
if (!Providers.globalProvider) {
Providers.globalProvider = new SharePointProvider(this.context);
}
}
public render(): void {
this.domElement.innerHTML = `
<div class="${ styles.calendar }">
<mgt-agenda></mgt-agenda>
</div>`;
}
// ...
}
Notice, how Microsoft Graph Toolkit uses the current context from SharePoint Framework and abstracts away retrieving calendar events from Microsoft Graph and showing them in your app to a single line of code: <mgt-agenda></mgt-agenda>
.
Microsoft Graph Toolkit connects to Microsoft Graph to retrieve data stored in Microsoft 365. To access the data, the service principal, that facilitates the communication with APIs, will require the necessary API permissions for Microsoft Graph. The necessary permissions for your project depend on which toolkit components you use. For more information about the Microsoft Graph permissions required by components, see their documentation.
After you have determined the list of API permissions needed for your SharePoint Framework project, add them to the project's configuration.
The next step is to add Microsoft Graph Toolkit to your project. For the exact steps, see the SharePoint Framework library for Microsoft Graph Toolkit documentation.
Important
If you use React in your SharePoint Framework project, you'll need to install both @microsoft/mgt-spfx
and @microsoft/mgt-react
packages in your project. You'll also need to adjust imports for the types that you load from the toolkit. For more information, see the React section of the SharePoint Framework library for Microsoft Graph Toolkit documentation.
After you complete the configuration steps, you can use Microsoft Graph Toolkit in your project.
SharePoint Framework web parts and extensions that use Microsoft Graph Toolkit show an error similar to:
Failed to execute 'define' on 'CustomElementRegistry': the name "mgt-xyz" has already been used with this registry.
This error is caused by multiple SharePoint Framework components trying to instantiate their own copy of the Microsoft Graph Toolkit. To avoid this issue, you should deploy the mgt-spfx.sppkg
package to the SharePoint app catalog in your tenant and update all solutions that use Microsoft Graph Toolkit to use the @microsoft/mgt-spfx
SharePoint package. For more information, see the SharePoint Framework library for Microsoft Graph Toolkit documentation.
SharePoint Framework web parts and extensions that use Microsoft Graph Toolkit don't work as expected. In the developer console, you see an error similar to:
AADSTS65001: The user or administrator has not consented to use the application with ID 'f96cafc5-5fa8-4594-9815-aa169a8c6d09' named 'SharePoint Online Client Extensibility Web Application Principal'. Send an interactive authorization request for this user and resource.
This error is often caused by the necessary Microsoft Graph API permissions not being granted to the SharePoint Framework service principal. In your SharePoint admin center, ensure that the necessary permissions are granted and listed on the API access page. For more information, see the SharePoint API management documentation.
Events
Microsoft 365 Community Conference
May 6, 2 PM - May 9, 12 AM
Skill up for the era of AI at the ultimate community-led Microsoft 365 event, May 6-8 in Las Vegas.
Learn moreTraining
Module
Get started with Microsoft Graph Toolkit - Training
Learn how to use Microsoft Graph Toolkit, a set of web components and authentication providers to connect your web app to Microsoft Graph and load data from Microsoft 365. You can use Microsoft Graph Toolkit in any JavaScript framework.
Certification
Microsoft Certified: Identity and Access Administrator Associate - Certifications
Demonstrate the features of Microsoft Entra ID to modernize identity solutions, implement hybrid solutions, and implement identity governance.