Using Microsoft Graph API inside Microsoft Flow in O365
Using Microsoft Graph API inside Microsoft Flow in Office 365 - Banner
Back

Using Microsoft Graph API inside Microsoft Flow in Office 365

Introduction

In this article, I will create a flow, which sends email alerts to the author of a modern page in SharePoint Online if other users commented the page. This is not an out-of-the-box functionality.

There are challenges I met in this simple task:

Comments list is hidden and you will not see it in MS Flow actions.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 936500

For notifying an author of the page, you need to receive a parent item. The standard “Site Pages” library is not presented in MS Flow actions.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 477039

“Get item” action doesn’t work with dynamic values. You can pass ListId and ItemId parameters into this action, but there is no output you can use in next actions.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 998127

Finally, I found a solution how to use MS Graph API to complete this flow demonstrated below:

  • Creating and registering an Office 365 app with access permissions to MS Graph API.
  • Authentication/Authorization call from MS Flow using custom app credentials from p.1.
  • Calling MS Graph API from MS Flow using token from p.2.

I have described the whole process in the following seven steps.

Step 1. Starting a flow

First, you need to find a name of the hidden comments list. Open SharePoint Designer and connect to your site using the owner’s credentials. You can find the list title as shown below:

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 282564

Now, you can create the first step of your new MS flow:

Open MS Flow portal and log in to your account.Choose My Flows -> Create from a blank.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 862572

Choose a trigger “When item is created”

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 966884

Specify the site URL and comments list title.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 976864

Step 2. Register Office 365 App with Graph API permissions

First, you need to understand what methods of Graph API you need. I use Graph API explorer to find necessary methods:

I need permissions Sites.Read.All and User.Read.All to call these Graph API methods.

An app registration process:

Go to App Registration portal and sign in with your Office 365 account.Press “Add an App” and type the name of your app. Push the Create

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 041926
Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 512391

In properties page press “Generate new password” button. Copy-paste and save app secret for later usage.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 371125
Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 295626

Add platform to the app:

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 845772

Specify URLs for the web platform. You can specify any URLs, because they are needed only for the first app authentication.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 455062

Add permissions Sites.Read.All and User.Read.All to your app and save changes.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 410649
Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 534106
Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 903592

The app registration is completed!

Now, we need to authenticate an app and receive the administrator’s consent. Before making a call /adminconsent endpoint we need to know the tenant ID of your Office 365.

Step 3. Retrieving Office 365 tenant ID.

Go to the Admin Portal of Office 365 using admin credentials.Expand “Admin centers” and click on “Azure AD”.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 387381

In Azure portal, choose “Azure Active Directory” -> Properties. A directory ID text field contains tenant ID. Copy it and save to use it later.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 273834

Step 4. Authentication app using admin consent.

Open a browser and paste this URL. Replace {tenant} with tenant ID from step 3 and {appid} with app\client Id from step 2.Sign in as Office 365 administrator.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 103410

Review app permissions and press “Accept”:

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 757538

You will see a “Bad request” window. It’s normal, because MS identity provider tries to send a response to redirect_uri=http://localhost/commentsalertsflow. Don’t worry – the app got the admin consent.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 321679

If you use a Fiddler or Browser dev tools, you able to see success response like:

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 390513

Step 5. Get OAuth 2 access token inside MS Flow.

Before starting using Graph API we need to receive site id using Graph API method “Get a site resource”:

Open Graph API explorer and sign-in using your O365 account.Choose method “SharePoint site based on relative path of this site”:

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 836959

Use your host name and relative path and run query.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 401433
Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 303581

Save if of the site for future usage.

Go back to MS Flow and try to call Graph API using HTTP action (this way does not work, skip it):

Click an Add action and choose HTTP.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 191871
Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 938986

Save and try to run a flow (add any comment on the modern page of the site).

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 061642

You see, a standard method of authorization doesn’t work.

Therefore, let’s try to retrieve access token in a separate http call:

Check an Authorization request in Postman:

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 123681

POSTHeaders:Content-Type: application/x-www-form-urlencodedBody:client_id={clientid}
&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
&client_secret={secret}
&grant_type=client_credentials
Go back to MS flow and add HTTP action. Don’t forget to rename the action! You will not be able to rename it if you use an output of the action in next actions.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 840701

Add JSON action. Set content as Body from the previous action. Click “Use sample payload to generate schema” and paste JSON result from Postman (p.1).

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 819354
Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 206208

Congratulations! You can proceed to the next step and use OAuth token in Graph API calls.

Step 6. Using Graph API inside MS Flow.

After the Parse Bearer Token action, add a new HTTP action: Call Graph API method “Get item in a list” with parameters ListId and ItemId. Add a header “Authorization=token_type access_token” (these parameters came as output of the “Parse Bearer Token” action).

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 436751

Add Parse JSON action and add schema using sample result from Postman. I removed redundant fields from Schema, because I had seven “id” fields in the output and only one is id of creator. So, in JSON schema I left only “id”, which belongs to “createdby”.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 158397

Call Graph API “Get a user” via HTTP action to receive the author email of the page and add Parse JSON action.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 181530

Add Send Email action:

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 028749

Save the flow. It looks like this:

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 936118

Step 7. Test the flow.

Open a target site and create a modern page or open the existing one.Post a comment.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 623299

View the results.

Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 484876

Conclusion

In terms of software engineering, Microsoft Flow becomes a workflow tool which can replace SharePoint workflows. Comparing to what was 6 months ago, MS Flow now has more features which allow calling external API. In addition, Microsoft Graph API enables communication between different apps of Office 365.

I hope you enjoyed this quick tutorial and learned how to use Microsoft Graph API inside Microsoft Flow in Office 365.

What is Microsoft Graph API?

Microsoft Graph API is an API platform for developers connecting to Office 365, Windows 10, EMS and providing a seamless access to all data stored in Azure or Office 365 from multiple MS cloud services. Integrating multiple services and devices, Graph API allows building a high-productive application across Windows, iOS, and Android platforms.

Next Article

We have a solution to your needs. Just send us a message, and our experts will follow up with you asap.

Thank you!

We have received your request and will contact you back soon.