Skip to content

Commit

Permalink
Merge pull request #794 from Sandeep-FED/dev
Browse files Browse the repository at this point in the history
add new script sample for retrieving sp site page visitors
  • Loading branch information
pkbullock authored Jan 2, 2025
2 parents 2d43ca8 + 78fc8f7 commit 1d1893c
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
90 changes: 90 additions & 0 deletions scripts/spo-get-sp-site-page-viewers-details/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
plugin: add-to-gallery
---

# Retrieve & Store SharePoint Site Pages Viewers Details

## Summary
This sample shows how to retrieve and store SharePoint site pages viewers' details using Exchange Online and PnP PowerShell.

## Prerequisites

Before running this script, ensure the following:

1. **PnP PowerShell Installed**:
- Install the latest version of [PnP PowerShell](https://pnp.github.io/powershell/articles/installation.html).
- Example: `Install-Module -Name PnP.PowerShell`.

2. **Exchange Online Module Installed**:
- Install the Exchange Online module.
- Example: `Install-Module -Name ExchangeOnlineManagement`.

3. **App Registration**:
- Create an Azure AD app registration with the appropriate API permissions (e.g., `AuditLog.Read.All` for Microsoft Graph and `Sites.ReadWrite.All` for SharePoint).

4. **PowerShell Environment**:
- Ensure PowerShell 7 or higher is installed for cross-platform compatibility.

5. **List Creation**:
- A SharePoint list (e.g., `AuditLog`) must exist in the specified site to store the retrieved data. The list should include the following fields:
- **User** (Single line of text)
- **Timestamp** (Date and Time)
- **Action** (Single line of text)

---


## [PnP PowerShell](#tab/pnpps)

```powershell
try {
# Configurations
$siteUrl = "https://contoso.sharepoint.com/sites/DemoSite" # SharePoint site URL
$listName = "AuditLog" # List name
$startDate = (Get-Date).AddDays(-90) # Fetch data from the past 90 days
$endDate = (Get-Date) # Until today
$clientId = "your-app-client-id"
# Authentication to Exchange Online
Connect-ExchangeOnline -UserPrincipalName "[email protected]"
# Fetch Audit Data
$auditRecords = Search-UnifiedAuditLog -StartDate $startDate -EndDate $endDate -Operations "PageViewed" -SiteIds "your-site-id"
if ($auditRecords) {
# Connect to SharePoint
Connect-PnPOnline -Url $siteUrl -ClientId $clientId -Interactive
# Add Records to List
foreach ($record in $auditRecords) {
Add-PnPListItem -List $listName -Values @{
"User" = $record.UserIds
"Timestamp" = $record.CreationDate
"Action" = $record.Operations
}
}
Write-Host "Audit data successfully added to the SharePoint list."
Disconnect-PnPOnline
} else {
Write-Host "No records found for the specified date range."
}
} catch {
Write-Error "An error occurred: $_"
} finally {
Disconnect-ExchangeOnline -Confirm:$false
}
StartProcessing
```
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
***

## Contributors

| Author(s) |
|-----------|
| [Sandeep P S](https://github.com/Sandeep-FED) |


[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-get-sp-site-page-viewers-details" aria-hidden="true" />
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions scripts/spo-get-sp-site-page-viewers-details/assets/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[
{
"name": "spo-get-sp-site-page-viewers-details",
"source": "pnp",
"title": "Retrieve & Store SharePoint Site Pages Viewers",
"shortDescription": "This script retrieves and stores SharePoint site pages viewers' details.",
"url": "https://pnp.github.io/script-samples/spo-get-sp-site-page-viewers-details/README.html",
"longDescription": [
"This sample shows how to retrieve and store SharePoint site pages viewers' details using Exchange Online and PnP PowerShell."
],
"creationDateTime": "2025-01-01",
"updateDateTime": "2025-01-01",
"products": ["SharePoint", "Exchange Online"],
"metadata": [
{
"key": "PNP-POWERSHELL",
"value": "1.11.0"
}
],
"categories": ["Audit", "Reports"],
"tags": [
"SharePoint Online",
"Exchange Online",
"Search-UnifiedAuditLog",
"Connect-PnPOnline",
"Add-PnPListItem"
],
"thumbnails": [
{
"type": "image",
"order": 100,
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/spo-get-sp-site-page-viewers-details/assets/example.png",
"alt": "Preview of the sample"
}
],
"authors": [
{
"gitHubAccount": "Sandeep-FED",
"pictureUrl": "https://github.com/Sandeep-FED.png",
"name": "Sandeep P S"
}
],
"references": [
{
"name": "More about PnP PowerShell",
"description": "Learn more about PnP PowerShell and its cmdlets.",
"url": "https://aka.ms/pnp/powershell"
}
]
}
]

0 comments on commit 1d1893c

Please sign in to comment.