In this article, we’ll talk about Microsoft Graph APIs and will show you a quick preview of the essential features. This technology is growing up too fast so some existing features may not be longer available at the time of reading and surely new features will be added to it after the time of writing.
Microsoft Graph API – formerly known as Office 365 unified API – is the new service-oriented architecture owned by Microsoft to allow developers to access a vast amount of data from the Microsoft cloud platforms. Microsoft web API is essentially designed to collaborate with Office 365 and some other services hosted on the MS Azure cloud platform.
The Microsoft Graph APIs expose RESTful services. This makes it very flexible because REST is compatible with almost any modern platform programming language. The exchange format is essentially in JSON and it can be encapsulated into the HTTPS transport security. The central point of Microsoft Graph is the concept of User, the authentication and authorization follow the OAuth2.0 mechanism making the user and role management to be standard for developers.
At the time of writing, Microsoft Graph API v1.0 is a stable revision that fulfills all production qualifications. A beta revision is also available in parallel for the latest releases, however, its main purpose is for testing new functionalities by the early adopter, and in any way, it is not recommended to be deployed into the production environment.
As stated earlier Microsoft Graph API is using RESTful services. You need to have first a basic background about how REST is working. The next is to have a good grasp on how to build queries using REST sauce. This part is pretty the same thing as doing ADO.NET services queries. And last but not least one is to know the REST resource URL. For example, if you want to retrieve all the contacts for a given user, you should be aware of the exact resource URL matching your expectation. You may be noticed that those requirements remain valid regardless of any programming languages. After the server return back to your apps, you need to write some codes in your favorite languages to parse JSON format in order to extract entities you may need to use. This is how it works at a basic level.
Microsoft, fortunately, provides language-specific SDK to communicate with Microsoft Graph in the test of object-oriented programming. If you’re going on that way, the SDK is managing automatically the whole process. Thus you’re not going to deal with basic HTTP requests, instead, you’re dealing directly with the platform object representation. The following technologies have a good integration within Microsoft Graph: ASP .NET MVC, Universal Windows platform, Php, Angular, Node.js, Ruby, and for mobile platforms Android, iOS Swift, iOS Objective-C, and Xamarin. We will show you a code snippet later on.
From the beginning as his being, Microsoft always took care of developer’s life and this time Microsoft provides them with a very helpful online utility name Graph Explorer to allow them to test their REST queries in the easiest way that ever existed. You can experiment with your own data on the cloud. The principle is done in 2 steps: sign in with your Microsoft Account, create your app credentials and secrets then run any queries you want in the URL query bar. It is available at https://developer.microsoft.com/en-us/graph/graph-explorer
Before accessing the services – setting up account credentials
From your Microsoft account (for example outlook.com ) you need to get an appID and secret from the Microsoft Application Registration portal. The appID and secret are used to identify your profile each time a request is made by the Microsoft Graph API. If you are familiar with Facebook / Twitter / Linkedin / Google plus Application development the same mechanism is used here to access remote resources. If you omit this step or give a wrong AppID/secret the service will throw a notification with the corresponding error message.
Microsoft Graph API allows developers to build rich apps using a variety of data sources. Under the hood, it is more than a simple tool to access cloud storage. Microsoft Graph indeed comes with high-level interfaces and endpoints to perform business intelligence data analysis. This feature is very helpful if you are rowing in machine learning analysis or in a need of high-quality level reports and dashboard plots. It comes with a bunch of insights algorithms for mining predictions and forecast purposes.
We enumerate below the essential components of Microsoft Graph that can be accessed using the API:
This last feature is the central point of interest because Microsoft Graph APIs are able to talk with the Active directory system either on-premise (using Federated as authentication type )or Azure active directory services (authentication type is Managed in this case). We manage here all permissions and authorizations for each user or group in the system by granting to or revoking from them a specific role.
We will create a Microsoft Outlook account, populate it with some data like the user’s profile, add some contacts with their respective data, create events, and add bookmarks on the user’s calendar. Afterward, we will use Microsoft Graph API to extract those data using the Microsoft Graph test tools. We’re showing you how to perform your first step into it. The process is still the same when querying data from the MS cloud.
In this example, we created the account of johndoeu201711 and then we add extra details on the user’s profile. The figure below shows all outlook components.
We also added 02 persons to his contact list. On his calendar, we create a new event. Next, we go to his OneDrive space and create there an excel file named Exel1.xls. We also create a new Outlook task from the Tasks menu.
We go to the Microsoft Application Registration and get our appID and secret from https://apps.dev.microsoft.com. The platform will show you your secret key, this key is private and you need to note it in a safe place.
Once the secret key is generated, you can finalize the last step as seen in the figure below.
It’s time to test the Microsoft Graph using the Graph Explorer online tools https://developer.microsoft.com/en-us/graph/graph-explorer
At the first login, Microsoft Graph is showing the permissions he needs. If you’re ok, hit the Yes button to go to the next step
The Graph Explorer dashboard:
Query tools:
This option is used to specify the current REST verb.
V1.0 is selected because we want to use the stable revision of the Microsoft Graph API, the other option is “beta”.
The URL bar is the place where you enter the REST resource you want to fetch
Some basic operations:
To get your profile: select GET verb and use the address https://graph.microsoft.com/v1.0/me/
To get all the account message, run the query https://graph.microsoft.com/v1.0/me/messages
In the Response Preview pane, you can see all the messages in JSON format.
To get all our contacts, run the query https://graph.microsoft.com/v1.0/me/people
To list the content of the user’s one drive https://graph.microsoft.com/v1.0/me/drive/root/children
To list the user’s events https://graph.microsoft.com/v1.0/me/events
We can also search a specific file stored in One Drive storage by name:
To search the created excel file named ‘Exel.xls’, run the query https://graph.microsoft.com/v1.0/me/drive/root/search(q=’Exel‘)
The MS Graph API will return all the matching files with metadata like the creation date, the path, the size, the last user modifying the file and the modification date, the file URL, etc.
The graph API example below shows how to use C# to send emails using Microsoft Graph API C# SDK.
public async Task SendEmail() { // Arrange. GraphService graphService = new GraphService(); string subject = "Test email from ASP.NET 4.6 Connect sample"; string bodyContent = "<html><body>The body of the test email.</body></html>"; List<Recipient> recipientList = new List<Recipient>(); recipientList.Add(new Recipient { EmailAddress = new EmailAddress { Address = userName } }); Message message = new Message { Body = new ItemBody { Content = bodyContent, ContentType = BodyType.Html, }, Subject = subject, ToRecipients = recipientList }; // Act Task task = graphService.SendEmail(client, message); // Assert Task.WaitAll(task); }
The GraphService is the main wrapper to interact with remote services, the method SendEmail will send all the parameters to the service.
Microsoft Graph is a well-documented project. As a Microsoft product, you have full support from Microsoft and the official website contains a lot of code snippets for different programming languages. Many enthusiastic developers also contribute to the SDK and many source codes are available on GitHub.
Data location and data variety are now the heart of the digital world. Even the smallest information is so precious and mining them is the gold rush for almost all digital companies. With Microsoft Graph and its API coupled with his favorite programming language, the developer can grab all those data, integrates them into their business rules, or cross join them to extract all valuable pieces of information to deliver profitable tools.
This is exactly the goal of Microsoft Graph. Finally, you should be aware that according to recent statistics, 85% of all fortune 500 companies use Microsoft Graph in their system. This is real proof that Microsoft Graph is a very promising technology for the next years.