Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#Script to fix the user mapping issues from page modernization
$SiteUrls = @(
"SGA"
)
$shouldFix = $true
try {
$checkedoutPages = @()
foreach($SiteUrl in $SiteUrls) {
Write-Host -ForegroundColor Cyan "Applying to $SiteUrl..."
$FullSiteUrl = "https://YOURDOMAIN.sharepoint.com/sites/$SiteUrl"
Connect-PnPOnline $FullSiteUrl -ErrorAction Stop
$pages = Get-PnPListItem -List "Site Pages" -Query "<View><Query><Where><Eq><FieldRef Name='PromotedState'/><Value Type='Number'>0</Value></Eq></Where></Query></View>"
Write-Host -ForegroundColor Green " Found $($pages.Count) pages for $SiteUrl"
foreach($page in $pages) {
Write-Host -ForegroundColor Green " $SiteUrl Page: $($page.FieldValues.Title)"
$csPage = Get-PnPClientSidePage -Identity $page.FieldValues.FileLeafRef
$authors = ConvertFrom-Json $csPage.PageHeader.Authors
if($authors.Count -eq 0) {
Write-Host -ForegroundColor Cyan " No authors found for page, skipping!"
}
if($authors.Count -eq 1) {
if(-Not $authors[0].id.EndsWith("@YOURDOMAIN.com")) {
if($page.FieldValues.CheckoutUser -ne $null) {
Write-Host -ForegroundColor Yellow " Checked out!"
$checkedoutPages += @{
Site = $SiteUrl;
Page = $page.FieldValues.FileLeafRef;
Title = $page.FieldValues.Title;
}
} else {
if($shouldFix) {
Write-Host -ForegroundColor Cyan " Fixing author: $($authors[0].id)"
$lwc = $page.FieldValues.LayoutWebpartsContent
$fixedLWC = $lwc.Substring(0,$lwc.IndexOf("&quot;",$lwc.IndexOf("|membership|")+12)) + "@YOURDOMAIN.com" + $lwc.Substring($lwc.IndexOf("&quot;",$lwc.IndexOf("|membership|")+12))
$fixedLWC2 = $fixedLWC.Substring(0,$fixedLWC.IndexOf("&quot;,",$fixedLWC.IndexOf("&quot;upn&quot;&#58;")+20)) + "@YOURDOMAIN.com" + $fixedLWC.Substring($fixedLWC.IndexOf("&quot;,",$fixedLWC.IndexOf("&quot;upn&quot;&#58;")+20))
Set-PnPListItem -List "Site Pages" -Identity $page.Id -SystemUpdate -Values @{"LayoutWebpartsContent"=$fixedLWC2} -ErrorAction Stop | Out-Null
Set-PnPClientSidePage $page.FieldValues.FileLeafRef -Publish -ErrorAction Stop | Out-Null
} else {
Write-Host -ForegroundColor Cyan " Should be fixed"
}
}
} else {
Write-Host -ForegroundColor Cyan " Author is just fine, skipping"
}
}
if($authors.Count -gt 1) {
Write-Host -ForegroundColor Yellow " More than one author found, fix it manually!"
}
}
Disconnect-PnPOnline
}
$checkedoutPages | ForEach-Object -Process {New-Object PSObject -Property $_} | Export-Csv -Path "checkedoutPages.csv" -NoTypeInformation
}
catch {
Write-Host -ForegroundColor Red "Exception occurred!"
Write-Host -ForegroundColor Red "Exception Type: $($_.Exception.GetType().FullName)"
Write-Host -ForegroundColor Red "Exception Message: $($_.Exception.Message)"
[console]::Beep(2000,1500)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.