If you want to know SharePoint Online Site Activity (like, when the Site has been readed or modified) you can use Microsoft Graph API to get site last activity date.
By using following SharePointSiteUsageDetail from Microsoft Graph REST API
GET /reports/getSharePointSiteUsageDetail(period='{period_value}')
GET /reports/getSharePointSiteUsageDetail(date={date_value})
you can get lot of details about the SharePoint Online Site, for example:
- Report Refresh Date
- Site Id
- Site URL
- Owner Display Name
- Is Deleted
- Last Activity Date
- File Count
- Active File Count
- Page View Count
- Visited Page Count
- Storage Used (Byte)
- Storage Allocated (Byte)
- Root Web Template
- Report Period
See detailed instructions from Microsoft Graph REST API reference: https://docs.microsoft.com/en-us/graph/api/reportroot-getsharepointsiteusagedetail?view=graph-rest-1.0
Using Microsoft Graph API in PowerShell
If you are already familiar with PowerShell, you might know that you can use Invoke-RestMethod command in PowerShell.
Before you can call Microsoft Graph API in your SharePoint Online tenant, you have to set application permission to call API. If you are not familiar about the Graph API authentication and permissions, see more information from Graph API Authentication overview provided by Microsoft.
There is also instructions in a GitHub how to connect the the Microsoft Graph using Application Permissions.
Once you have set Application Permissions, you can start using Microsoft Graph API in PowerShell.
Getting SharePoint Site Activity using PowerShell
When you are working with Invoke-RestMethod and Microsoft Graph API you have to handle authentication. This can be done by using authorization bearer and access token. I followed this instruction written by Rudy Mens to get authorization done using my application permissions.
Get all sites and their Last Activity Date
Here is my code to get all sites from SharePoint Online tenant and display their last activity date.
#Get sharepoint site activity from Graph Api
$String = "https://graph.microsoft.com/beta/reports/getSharePointSiteUsageDetail`(`period=`'`D180`'`)?`$format=application/json"
$Results = Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Method Get -Uri $String
$Results.value | Sort-Object -Property lastActivityDate | Select-Object -Property siteUrl, lastActivityDate, rootWebTemplate | ForEach-Object {
write-Host "------"
$_.siteUrl
write-Host "Last activity date:" $_.lastActivityDate
$_.rootWebTemplate
if (!$_.lastActivityDate) {write-host "Last activity date not found" -ForegroundColor Yellow }
}
Note! Sometimes the last activity date cannot be found from the report, that’s why i put the if-clause when last activity date is null or empty.
Get all Office 365 groups and their Last Activity Date
If you like get Office 365 Group Last Activity Date, you can use getOffice365GroupsActivityDetail found from Microsoft Graph API.
Here is my code to Get All Office 365 Groups from the tenant and display their last activity date.
#Get Office 365 Group activity from Graph Api
$String = "https://graph.microsoft.com/beta/reports/getOffice365GroupsActivityDetail`(`period=`'`D180`'`)?`$format=application/json"
$Results = Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Method Get -Uri $String
$Results.value | Sort-Object -Property lastActivityDate | Select-Object -Property groupDisplayName, lastActivityDate | ForEach-Object {
write-Host "------"
$_.groupDisplayName
write-Host "Last activity date:" $_.lastActivityDate
#If lastActivityDate is null or empty
if (!$_.lastActivityDate) {write-host "Last activity date not found" -ForegroundColor Yellow }
}
Conclusions
Using Microsoft Graph API in PowerShell, you can get lot of information about the SharePoint Sites.
In the next post I will show how to set Site collection to Read Only state based on Site’s Last Activity Date and using SharePoint Site Policy feature.
How can we import site usage details for 1 specific site from Sharepoint using Graph API.
LikeLike
Script in this blog will get site usage details from all SharePoint site collections. But you can put If-clause to ForEach-Object loop and specify site URL there for filtering one specific site.
LikeLike