How to Get Microsoft 365 Usage Reports Using Microsoft Graph in Power Automate?

In Power Automate, I received a requirement from a client asking if it’s possible to generate detailed Microsoft 365 usage reports daily, weekly, or monthly, specifically for SharePoint Online sites.

While SharePoint provides a ‘Site Usage’ feature to view usage reports, they wanted to export this information into an Excel or CSV file. The report would track which users accessed the site, usage statistics, total number of files, visits, views, and more.

After researching various approaches, I found that Microsoft Graph API can be used to achieve this. In this tutorial, I will show how to get Microsoft 365 usage reports using Microsoft Graph in Power Automate.

Additionally, we will discuss how to download the microsoft 365 usage reports using Microsoft Graph Api in Power Automate.

Get Microsoft 365 Usage Reports Using Microsoft Graph API in Power Automate

Before creating the flow, you must create an app registration in your Azure AD. I have already created the app in Azure AD.

Create App Registration in your Azure AD

To make sure the App Registration has the right access, follow these steps:

  • Check that the API permissions include Reports.Read.All under Application permissions.
  • Ensure that an admin has approved these permissions.
How to automate a usage report for my SharePoint hub

Under Certificates & secrets, click on + New client secret. Provide a description, choose an expiration period (e.g., six months, one year), then click Add. A Value for the new client secret will be generated. Copy and save this value right away because you will need it in Power Automate, and you won’t be able to see it again after leaving the page.

View usage data for your SharePoint site using Power Automate

Note:

It can take a couple of hours for these permissions to take effect. So if your Power Automate flow doesn’t work right away, give it a few hours for the permissions to fully apply.

Now follow the below steps:

1. Navigate to the Power Automate Home page, click + Create, and select the Scheduled Cloud Flow. Then provide the following information:

  • Starting: Provide on which date you want to run your flow.
  • at: Provide the time you want to run the flow, in my case, at 10:00 AM.
  • Repeat every: We want to run the flow on Friday every week.
Microsoft 365 admin center SharePoint site usage reports in Power Automate

2. Next, I created three Initialize variable actions to store the Tenant ID, Client ID, and Client Secret Value. All three variables are of type String. In the Value field of each variable, enter the corresponding values:

  • Tenant ID – Provide your Tenant ID.
  • Client ID – Provide your Client ID.
  • Client Secret Value – Provide your Client Secret Value.
Get usage data from SharePoint Online using Power Automate

3. Add a compose action and provide all of the M365 usage report request urls you want to download reports for:

[
"https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageDetail(period='D7')",
  "https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageFileCounts(period='D7')",
  "https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageSiteCounts(period='D7')",
  "https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageStorage(period='D7')",
  "https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsagePages(period='D7')",
  "https://graph.microsoft.com/v1.0/reports/getSharePointActivityUserDetail(period='D7')",
  "https://graph.microsoft.com/v1.0/reports/getSharePointActivityFileCounts(period='D7')",
  "https://graph.microsoft.com/v1.0/reports/getSharePointActivityUserCounts(period='D7')",
  "https://graph.microsoft.com/v1.0/reports/getSharePointActivityPages(period='D7')"
]
How to access Site Usage Reports in SharePoint Online using Power Automate

4. Next, I added an HTTP action (Premium Connector) using the POST method to generate an Access Token with application permissions. This access token will be used in the headers of subsequent Microsoft Graph API calls to download Microsoft 365 usage reports.

Provide the below parameter in the HTTP action:

  • URI: In the URI field, use the following format:
https://login.microsoftonline.com/@{variables('varTenantID')}/oauth2/token
  • Method: Select POST from the drop-down
  • Headers: Add the following key-value pair:
Key: Content-Type
Value: application/x-www-form-urlencoded
  • Body: provide the following:
client_id=@{variables(' varClientID')}&client_secret=@{variables('varClientSecretValue')}&resource=https://graph.microsoft.com&grant_type=client_credentials&
How Can I automatically export the Excel file for the Site Usage using Power automate

5. Add an Apply to each action. In the ‘Apply to each’ input field, use the following expression:

array(outputs('Compose'))
how get sharepoint site usage data using power automate

6. Next, inside the loop, add an HTTP action using the below parameter:

  • URI: provide the current item from dynamic content (which will be the URL for the usage report)
  • Method: Select GET from the drop-down
  • Headers: Add the following key-value pair:
Key: Authorization
Value: "Bearer @{body('HTTP_|_Login_Microsoft_Graph')['access_token']}"
power automate get sharepoint site usage data

This will use the access token generated earlier to authenticate the request to Microsoft Graph.

7. Add a new HTTP action after the HTTP | Usage Report action within the Apply to each loop. Then provide the below parameter:

  • URI: Put the below expression:
@{outputs('HTTP_|_Usage_Report')['headers']['location']}
  • Method: Select GET from the drop-down
get sharepoint site usage data using power automate

8. Select the HTTP action, open its settings, and expand Run after. Check the box for ‘has failed.’

How to See SharePoint Site Usage Data using power Automate

9. Then I add the create file action and provide the below parameter:

  • Site Address: Choose the SharePoint site from the dropdown.
  • Folder Path: Select the folder from the Show Picker where you want to store the copied files.
  • File Name: provide below expression:
concat(replace(first(split(item(),'(')),'https://graph.microsoft.com/v1.0/reports/',''),'-',
    formatDateTime(utcNow(), 'ddMMyyyyHHmmss'),
    '.csv'
)
Power Automate Site Usage Reports in SharePoint Online

Run the Flow to Get Microsoft 365 Usage Reports Using Microsoft Graph

Now, it’s time to save the flow. Click on Test -> Select Manually -> click Test. After the flow runs successfully, go to the SharePoint library, and you will see that the file has been successfully created.

How to Get Microsoft 365 Usage Reports Using Microsoft Graph in Power Automate

You can download microsoft 365 usage reports using microsoft graph api in Power Automate.

I hope you follow all the steps using Power Automate and Microsoft Graph API. You can automate downloading Microsoft 365 usage reports; using this flow, you can schedule, saving you time and effort in manually retrieving usage data.

Also, you may like some more Power Automate tutorials:

>

Build a High-Performance Project Management Site in SharePoint Online

User registration Power Apps canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial FREE PDF Download

FREE Power Platform Tutorial PDF

Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…