Working with Microsoft Graph in SharePoint Framework – Part 1


In this blog series you will learn about Microsoft Graph and how you can use it to develop advanced solutions to solve business problems. we will start from basics and then discuss advanced concepts associated with Microsoft Graph.

Overview of Microsoft Graph

Microsoft Graph provides a unified programmability model that you can use to access data in Office 365, Windows 10, and Enterprise Mobility + Security.

Microsoft Graph is a RESTful web API that serves as a single proxy to Microsoft 365 cloud services such as Delve, Excel, Microsoft Teams, SharePoint, OneDrive etc. through a single endpoint, https://graph.microsoft.com.

Major Services and features in Microsoft Graph (Image source: Microsoft)
Why should I use Microsoft Graph?

Let’s start with a simple example, If you need to develop a SharePoint WebPart that will display upcoming 5 events scheduled in your outlook calendar, How can you get this information? SharePoint REST API will allow you to query only SharePoint site but how can you get events stored in outlook calendar of a user?

Take another example where you need to develop a custom SharePoint Modern WebPart to show list of members and their profile picture who are part of a team in Microsoft Teams, how can you get this data?

Microsoft Graph will help you solve both the problems by querying a common endpoint provided without worrying about individual API for every service.

Open Microsoft Graph Explorer and view list of services that you can query in Microsoft Graph. Below image shows sample categories available in Microsoft Graph.


Use Microsoft Graph explorer to test API endpoints, to apply CRUD operations to a resource, you construct a RESTful API request as follows:

{HTTP method}https://graph.microsoft.com/{version}/{resource}?{query-parameters}

{HTTP method}: The HTTP method used on the request to Microsoft Graph.
{version}: The version of the Microsoft Graph API your application is using.
{resource}: The resource in Microsoft Graph that you’re referencing.
{query-parameters}: Optional OData query options or REST method parameters that customize the response.

Below Graph API endpoint is retrieving my emails with high importance.


Query Parameters

Microsoft Graph API supports many of the OData Protocol’s query parameters.

NameDescriptionExample
$select Only fetch required columnshttps://graph.microsoft.com/v1.0/users?$select=givenName,surname


$orderby
Use $orderby to specify sort-order of results https://graph.microsoft.com/v1.0/users?$orderby=displayName
$filter Use $filter to find resources that match a specified query Fetch all unread emails in the signed-in user’s inbox using the equals logical operator:
https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages?$filter=isRead eq false


$top
If you want to retrieve more items use $top query parameter to specify the page size of the result set. If more items remain in the result set, the response body will contain an @odata.nextLink parameter Get list of users in a page size of 5, you can retrieve next users by using value of the @odata:nextLink property to Microsoft Graph to retrieve next page of results https://graph.microsoft.com/v1.0/users?$top=5
$skip$skip query parameter to set the number of items to skip at the start of a collection Skips first 10 email in the mailbox:
https://graph.microsoft.com/v1.0/me/messages?$skip=10


$expand
Use $expand to fetch additional resources related to the requested resource Skips first 10 emails in the mailbox:
https://graph.microsoft.com/v1.0/me/messages?$skip=10


$count
Add $count=true as a query string parameter to include a count of the total number of items in a collection with the resultsFetch users collection alongwith the number of items in the user collection: https://graph.microsoft.com/v1.0/users?$count=true

In the next blog of this series you will learn how to optimize network performance, access user data & files with Microsoft Graph.

Summary

Subscribe to our blogs to stay updated on our future articles here. Feel free to get in touch with us if you need any assistance on Microsoft Graph at [email protected]