Navigate to the above-created directory.
Run Yeoman SharePoint Generator to create the solution.
Since AadHttpClient is in beta, use --plus beta with Yeoman generator.
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
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 onwards).
Selected choice - SharePoint Online only (latest).
Place of files
We can choose to use the same folder or create a subfolder for our solution.
Selected choice - Same folder
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and will be accessible everywhere.
Selected choice - N (install on each site explicitly).
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose web part option.
Selected choice - WebPart
Web part name
Hit enter to select the default name or type in any other name.
Selected choice - MSGraphAADHttpClient
Web part description
Hit enter to select the default description or type in any other value.
Selected choice - Consume MS Graph with SPFx using AADHttpClient
Framework to use
Select any JavaScript framework to develop the component. Available choices are (No JavaScript Framework, React, and Knockout)
Selected choice - React.
Yeoman generator will perform 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.
In the command prompt type the below command to open the solution in the code editor of your choice.
Access MS Graph using AadHttpClient
Microsoft Graph can be accessed by low-level type used to access Azure AD secured REST API (AadHttpClient). AadHttpClient client object can be used to consume any REST API, whereas MSGraphClient client object can only consume Microsoft Graph.
In the MsGraphAadHttpClient.tsx file under “\src\webparts\msGraphAadHttpClient\components\” folder, add the below import statement
- import { AadHttpClient } from '@microsoft/sp-http';
Permissions
In order to consume MS Graph or any third party REST APIs, we need to explicitly specify the permissions in the manifest of the solution.
In the package-solution.json file under “config” folder, configure the webApiPermissionRequests property to specify User.ReadBasic.All permission.
- {
- "$schema": "https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
- "solution": {
- "name": "spfx-msgraph-aadhttpclient-client-side-solution",
- "id": "f4213803-dc4b-42d9-b6c4-96e895ec02fe",
- "version": "1.0.0.0",
- "includeClientSideAssets": true,
- "webApiPermissionRequests": [
- {
- "resource": "Microsoft Graph",
- "scope": "User.ReadBasic.All"
- }
- ]
- },
- "paths": {
- "zippedPackage": "solution/spfx-msgraph-aadhttpclient.sppkg"
- }
- }
webApiPermissionRequests is an array of webApiPermissionRequest items where each item is defined as below:
- resource: name or the ObjectId (in Azure AD). E.g. Microsoft Graph
- scope: name or unique ID of the permission
Configure Props
Define the context property in IMsGraphAadHttpClientProps.ts under “\src\webparts\msGraphAadHttpClient\components\”
- import { WebPartContext } from '@microsoft/sp-webpart-base';
-
- export interface IMsGraphAadHttpClientProps {
- description: string;
- context: WebPartContext;
- }
Configure State
We will define the state for our React component.
To define interface to represent user, add file IUserItem.ts under “\src\webparts\ msGraphAadHttpClient\components\”
- export interface IUserItem {
- displayName: string;
- mail: string;
- userPrincipalName: string;
- }
Add file IMsGraphAadHttpClientState.ts under “\src\webparts\msGraphAadHttpClient\components\”
- import { IUserItem } from './IUserItem';
-
- export interface IMsGraphAadHttpClientState {
- users: Array<IUserItem>;
- }
Get User Details
Implement below method to get the user details from your tenant.
- private getUserDetails(): void {
- const aadClient: AadHttpClient = new AadHttpClient(
- this.props.context.serviceScope,
- "https://graph.microsoft.com"
- );
-
-
- aadClient
- .get(
- `https:
- AadHttpClient.configurations.v1
- )
- .then(response => {
- return response.json();
- })
- .then(json => {
-
- var users: Array<IUserItem> = new Array<IUserItem>();
-
-
- console.log(json);
-
-
- json.value.map((item: any) => {
- users.push( {
- displayName: item.displayName,
- mail: item.mail,
- userPrincipalName: item.userPrincipalName,
- });
- });
-
-
- this.setState(
- {
- users: users,
- }
- );
- })
- .catch(error => {
- console.error(error);
- });
- }
Enable Targeted Release on your Tenant
The MS Graph operation is part of an experimental feature and is only available in Targeted release (first release) tenants.
- Open Office 365 admin center
- Click Settings > Organization profile
- Click Edit against “Release preferences”
- Select the preference
Please refer to the article
here to set up your tenant for targeted release.
Test the WebPart
- On the command prompt, type “gulp serve”
- Open SharePoint site
- Navigate to /_layouts/15/workbench.aspx
- Add the web part to the page.
API Management
In the Production environment, after deploying the web part follow the below steps to approve API requests.
- Open SharePoint Admin Center (https://[tenant]-admin.sharepoint.com)
- Click “Try the preview”
- From left navigation, Click “API Management”
- Approve pending requests
Summary
Microsoft Graph offers a wide range of REST APIs to access content and services provided by Office 365. The MS Graph operation is part of an experimental feature and is only available in Targeted release (first release) tenants only. AadHttpClient is in preview mode, avoid using it in production.