Thanks to all of the attendees from my Intro to Microsoft Graph Development session from Dog Food Con 2019. I’ve presented here many times and I always enjoy the questions and engagement from local attendees. Below are my slides from the session. Feel free to reach out if you have questions or comments.
I have the privilege of presenting “Getting Started with Microsoft Graph Development” at the upcoming Collab365 SharePoint Summit taking place Sept 10-12, 2019. This is a free online conference with MVPs and experts from around the world presenting on developer, IT Pro, and adoption topics.
Title: Getting Started with Microsoft Graph Development
Abstract: “I hear that I need to use Microsoft Graph for developing against Office 365 but I have no clue where to start.” “I want to grant access to company data without throwing in the entire kitchen sink.” Fear not fellow developers and admins.
This session we will ramp you up to a 200 level knowledge on the pertinent parts of Microsoft Graph including endpoints available, syntax, authentication flows, and more. We will also cover useful examples of what can be accomplished using these APIs. Prior experience with Microsoft Graph is not required but can be helpful.
You can also purchase an all-access pass which includes lifetime access to the videos, additional e-books, and more. Looking forward to participating in this great event.
Microsoft Graph is the unified API for any developers working with data inside Office 365, Azure Active Directory (Azure AD), Windows 10, and more. In this post we’ll cover a quick introduction and share resources from 30 Days of Microsoft Graph blog series to show how to authenticate and to make calls against Microsoft Graph with C# and .Net Core (v2.1 as of the time of writing.) Each of the referenced articles aims to take 5-15 mins to get you up to speed as quickly as possible while also providing hands-on exercises. If you’d like to skip the background reading and start from scratch building a .Net Core console application that calls Microsoft Graph read through the README for the base-console-app within dotnetcore-console-sample.
<Update 2019-10-07>Thanks to reader John Guilbert for pointing out that the sample code using MSAL .Net 2.x has deprecated certain APIs. I’ve updated the sample code to reflect MSAL .Net 4.x.</Update>
Microsoft Graph overview
Microsoft Graph offers developers (and IT pros / admins) the ability to access data and insights in a number of services within Microsoft 365 services. This includes:
Azure AD
Office 365 services
SharePoint
OneDrive
Outlook/Exchange
Microsoft Teams
OneNote
Planner
Excel
Enterprise Mobility and Security services
Identity Manager
Intune
Advanced Threat Analytics
Advanced Threat Protection
Windows 10 services
Activities
Devices
Education
By providing a unified endpoint for accessing all of these services Microsoft Graph removes a number of barriers including:
Discovering the service-specific endpoint URL
Authenticating to each endpoint separately
Managing different permission models
Working with incompatible data formats
…and more
All requests made to Microsoft Graph are sent as REST calls to https://graph.microsoft.com and leverage a common authentication model based on Azure AD and OAuth permissions along with a consent framework for users or admins. The quickest way to see Microsoft Graph requests in action is to navigate to the Microsoft Graph explorer (https://aka.ms/ge, ge = Graph Explorer.) For more information on using Graph Explorer please read Day 3 – Graph Explorer from the 30 Days of Microsoft Graph series. Additionally you can make requests against Microsoft Graph using API development tools such as Postman. Please read Day 13 – Postman to make Microsoft Graph requests for more information on using PostMan with Microsoft Graph.
Getting started sample
Seeing requests and their responses in a browser or tool is useful, but making requests in code or scripts is the more common scenario for usage. In the examples below we will cover C# and .Net Core as .Net Core is available cross-platform, can be built in Visual Studio Code (also cross-platform), and offers many hosting options (console app, web app, serverless functions, and more.)
Authentication
All requests to Microsoft Graph require an authenticated context, either delegated or app-only. Delegated is a union of the logged-in user’s context along with the application’s context. App-only (as the name implies) is only the application’s context without any user involvement. Please read Day 8 – Authentication roadmap and access tokens and Day 9 – Azure AD applications on V2 endpoint for more information about creating an Azure AD application and getting an authenticated context. On a similar note, you are highly encouraged to leverage Microsoft Authentication Library (MSAL) for creating your authentication context as this is the forward-focused version as opposed to the older Active Directory Authentication Library (ADAL).
Microsoft Graph SDK
While it is entirely possible to call the Microsoft Graph with an HttpClient (or similar) object, the Azure AD Identity and Microsoft Graph product groups recommend leveraging the Microsoft Graph SDK (Microsoft.Graph on Nuget.) This SDK provides a number of benefits including:
Strongly typed entities and Microsoft Graph responses
Fluent API syntax
…and more
In future releases Microsoft Graph SDK will also provide abstractions for authentication prerequisites, automatic handling of retry logic or error handling, and more.
Sample solution
As mentioned at the beginning of this post if you’d like to build a working application from scratch (or clone the repo and configure the necessary settings) you can find the base-console-app within dotnetcore-console-sample. Extracting the bare essential lines of code from this sample results in the following for authenticating to Microsoft Graph.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
The following class implements the IAuthenticationProvider interface used for retrieving and then adding an Azure AD access token to subsequent requests to Microsoft Graph. An out of the box implementation of this class will be provided at a later date within the Graph SDK.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
In this blog post we covered a quick introduction of Microsoft Graph and linked to additional resource within the 30 Days of Microsoft Graph blog series for additional background reading. We also covered a barebones implementation of calling Microsoft Graph in a C# .Net Core console application. Full instructions can be found on the base-console-app within dotnetcore-console-sample. Thank you for reading along and please open an issue on GitHub repo if you run into any issues with the sample project. Enjoy the rest of The Second Annual C# Advent.
Thanks to all of the attendees from my Getting Up to Speed on Microsoft Graph Development session from SharePoint Saturday Twin Cities 2018. Below are my slides from the session. Feel free to reach out if you have questions or comments.
Last week myself and a virtual team of Microsoft and community contributors started a month long blog series called 30 Days of Microsoft Graph on the Microsoft Graph blog. You can read more about it on the announcement post which is being updated daily with links to each of the posts throughout November. Please see the below options for following along: