Microsoft recently announced that they are discontinuing the classic Stream and introducing a new version. This change necessitates the identification of Stream web parts across SharePoint pages within your organization. To help with this transition, you can use a PowerShell script to scan your SharePoint sites for any Stream web parts that may be in use. Below is a detailed explanation of how to achieve this using PowerShell and the PnP PowerShell module.
Script Overview
The script provided here connects to your SharePoint Online sites, iterates through all site pages, and checks for the presence of Stream web parts. It compiles a list of these pages and exports the information to a CSV file.
Key Components of the Script
1. Parameters and Global Collection Initialization
$PagesDataCollGlobal = @()
2. Function Definition CheckWebParts
This function connects to a given site, retrieves all site pages, and checks each page for Stream web parts.
Function CheckWebParts() {
Param (
$siteURL = $(throw "Please Enter Value:"),
$siteTitle = $(throw "Please Enter Value:")
)
Connect-PnPOnline -Url $siteURL -Interactive
$PagesDataColl = @()
$SitePages = Get-PnPListItem -List "Site Pages"
ForEach ($Page in $SitePages) {
$webpartFound = "No"
Write-Host $Page.FieldValues.FileLeafRef -ForegroundColor Green
$page = Get-PnPClientSidePage -Identity $Page.FieldValues.FileLeafRef
$webParts = $page.Controls
foreach ($webpart in $webparts) {
If ($webpart.Title -Contains "Stream") {
$webpartFound = "Yes"
}
}
$Data = New-Object PSObject -Property ([Ordered] @{
PageName = $Page.PageTitle
RelativeURL = $siteURL + "/SitePages/" + $Page.Name
FileName = $Page.Name
SiteURL = $siteURL
SiteName = $siteTitle
HasStream = ($webpartFound -eq "Yes" ? "Yes" : "No")
})
$PagesDataColl += $Data
If ($webpartFound -eq "Yes") {
Write-Host "Stream found" -ForegroundColor Yellow
}
}
return $PagesDataColl
}
3. Main Script Logic
The script connects to the root site, retrieves all subsites, and calls the CheckWebParts function for each site and its subsites recursively.
$SiteURL = "YOUR SITE URL"
$CSVFile = "C:\Temp\SitePages.csv"
Connect-PnPOnline -Url $SiteURL -Interactive
$sites = Get-PnPSubWeb
$PagesDataColl = @()
foreach ($site in $sites) {
$PagesDataCollGlobal += CheckWebParts -siteURL $site.URL -siteTitle $site.Title
Connect-PnPOnline -Url $site.Url -Interactive
$subsites = Get-PnPSubWeb
foreach ($siteTemp in $subsites) {
$PagesDataCollGlobal += CheckWebParts -siteURL $siteTemp.URL -siteTitle $siteTemp.Title
Connect-PnPOnline -Url $siteTemp.Url -Interactive
$subsites1 = Get-PnPSubWeb
foreach ($siteTemp1 in $subsites1) {
$PagesDataCollGlobal += CheckWebParts -siteURL $siteTemp1.URL -siteTitle $siteTemp1.Title
Connect-PnPOnline -Url $siteTemp1.Url -Interactive
$subsites2 = Get-PnPSubWeb
foreach ($siteTemp3 in $subsites2) {
$PagesDataCollGlobal += CheckWebParts -siteURL $siteTemp3.URL -siteTitle $siteTemp3.Title
Connect-PnPOnline -Url $siteTemp3.Url -Interactive
$subsites3 = Get-PnPSubWeb
foreach ($siteTemp4 in $subsites3) {
$PagesDataCollGlobal += CheckWebParts -siteURL $siteTemp4.URL -siteTitle $siteTemp4.Title
}
}
}
}
}
$PagesDataCollGlobal
# Export data to CSV File
$PagesDataCollGlobal | Export-Csv -Path $CSVFile -NoTypeInformation
Running the Script
- Install the PnP PowerShell Module: If you haven't already, install the PnP PowerShell module.
Install-Module -Name PnP.PowerShell
- Connect to Your SharePoint Online Site: Update the $SiteURL variable with your SharePoint site URL.
- Execute the Script: Run the script in a PowerShell window. Ensure you have the necessary permissions to access the SharePoint sites.
- Review the Output: The script will generate a CSV file at the specified path ($CSVFile), listing all pages with and without Stream web parts.
Conclusion
This script provides a systematic approach to identify Stream web parts across your SharePoint pages, helping you prepare for the transition to the new Stream. By exporting the results to a CSV file, you can easily review and manage the impacted pages.
Regards,
Keyur Pandya
Connect with me on LinkedIn