By using this web site you accept our use of cookies. More information about cookies
Accept
Infopulse - Expert Software Engineering, Infrastructure Management Services
By using this web site you accept our use of cookies. More information about cookies
Accept
Infopulse - Expert Software Engineering, Infrastructure Management Services
Infopulse - Expert Software Engineering, Infrastructure Management Services
Send message
Send message Please fill in this quick form and we will send you a free quote shortly.
* Required fields
Your privacy is important to us. We will never share your data.
Subscribe to our updates Be among the first to get exclusive content on IT insights, innovations, and best practices.
* Required fields
Your privacy is important to us. We will never share your data.
Subscribe to our Vacancies Please fill in this quick form to be among the first to receive our updates.
* Required fields
Your privacy is important to us. We will never share your data.
Subscribe to our updates Be among the first to get exclusive content on IT insights, innovations, and best practices.
* Required fields
Your privacy is important to us. We will never share your data.
Photo of Iegor Tsvietkov Send an email to Iegor Tsvietkov Please fill in this quick form to contact our expert directly.
* Required fields
Your privacy is important to us. We will never share your data.
Infopulse - Expert Software Engineering, Infrastructure Management Services
Read the Full Case Study Don't miss the most interesting part of the story!
Submit this quick form to see the rest and to freely access all case studies on our website.
* Required fields
Your privacy is important to us. We will never share your data.

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:

  1. Comments list is hidden and you will not see it in MS Flow actions.
  2. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 936500

  3. 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.
  4. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 477039

  5. “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.
  6. 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:

  1. Creating and registering an Office 365 app with access permissions to MS Graph API.
  2. Authentication/Authorization call from MS Flow using custom app credentials from p.1.
  3. 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:

  1. Open MS Flow portal and log in to your account.
  2. Choose My Flows -> Create from a blank.
  3. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 862572

  4. Choose a trigger “When item is created”
  5. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 966884

  6. Specify the site URL and comments list title.
  7. 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:

  1. Go to App Registration portal and sign in with your Office 365 account.
  2. Press “Add an App” and type the name of your app. Push the Create
  3. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 041926

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

  4. In properties page press “Generate new password” button. Copy-paste and save app secret for later usage.
  5. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 371125

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

  6. Add platform to the app:
  7. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 845772

  8. Specify URLs for the web platform. You can specify any URLs, because they are needed only for the first app authentication.
  9. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 455062

  10. Add permissions Sites.Read.All and User.Read.All to your app and save changes.
  11. 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.

  1. Go to the Admin Portal of Office 365 using admin credentials.
  2. Expand “Admin centers” and click on “Azure AD”.
  3. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 387381

  4. In Azure portal, choose “Azure Active Directory” -> Properties. A directory ID text field contains tenant ID. Copy it and save to use it later.
  5. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 273834

Step 4. Authentication app using admin consent.

  1. Open a browser and paste this URL. Replace {tenant} with tenant ID from step 3 and {appid} with app\client Id from step 2.
  2. Sign in as Office 365 administrator.
  3. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 103410

  4. Review app permissions and press “Accept”:
  5. 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”:

  1. Open Graph API explorer and sign-in using your O365 account.
  2. Choose method “SharePoint site based on relative path of this site”:
  3. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 836959

  4. Use your host name and relative path and run query.
  5. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 401433

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

  6. 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):

  1. Click an Add action and choose HTTP.
  2. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 191871

  3. Fill in the URL and parameters: Site Id, ListId, ItemId, tenant id, app id, secret.

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

  4. Save and try to run a flow (add any comment on the modern page of the site).
  5. 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:

  1. Check an Authorization request in Postman:
  2. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 123681

    POST

    Headers:

    Content-Type: application/x-www-form-urlencoded

    Body:

    client_id={clientid}
    &scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
    &client_secret={secret}
    &grant_type=client_credentials

  3. 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.
  4. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 840701

  5. 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).
  6. 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.

  1. 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).
  2. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 436751

  3. 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”.
  4. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 158397

  5. Call Graph API “Get a user” via HTTP action to receive the author email of the page and add Parse JSON action.
  6. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 181530

  7. Add Send Email action:
  8. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 028749

  9. Save the flow. It looks like this:
  10. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 936118

Step 7. Test the flow.

  1. Open a target site and create a modern page or open the existing one.
  2. Post a comment.
  3. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 623299

  4. View the results.
  5. Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 484876
    Using Microsoft Graph API inside Microsoft Flow in Office 365 - Infopulse - 052636

Conclusion

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.

Subscribe to our Newsletter