Created
June 7, 2021 15:54
-
-
Save ktskumar/e6104c388a912aba8d4e3ab385440861 to your computer and use it in GitHub Desktop.
Export all controls or webparts from all modern site pages in a site using Microsoft 365 CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Export all webpart or control information from all pages in a SharePoint site to CSV format | |
### | |
$outputfilepath = "<Output fodler>\webparts.csv" | |
$weburl = "https://contoso.sharepoint.com" | |
#Validate login in | |
$m365Status = m365 status | |
if ($m365Status -eq "Logged Out") { | |
m365 login | |
} | |
#Declare Array Object | |
$webpartValues = @() | |
### | |
Write-Host 'Get all pages from web: ' $weburl | |
$allpages = (m365 spo page list --webUrl $weburl -o json).Replace('Id', 'Idd') | ConvertFrom-Json | |
Write-Host 'Total Pages: ' $allpages.Length | |
### | |
# Loop through pages | |
foreach ($singlePage in $allPages) { | |
$a = $singlePage.CanvasContent1 | |
If ($null -eq $a) { | |
Write-Host 'No webpart available' | |
$pageObject = New-Object -TypeName PSObject | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Page Name' -Value $singlePage.Name | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Type' -Value $wp.type | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Title' -Value '' | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Control Type' -Value '' | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Order' -Value '' | |
} | |
else { | |
$webparts = (m365 spo page control list --webUrl $weburl --name $singlePage.Name -o json).Replace('listName', 'listNameId') | ConvertFrom-Json | |
Write-Host 'Webpart count for the page '$singlePage.Name ' - ' $webparts.Length | |
foreach ($wp in $webparts) { | |
$pageObject = New-Object -TypeName PSObject | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Page Name' -Value $singlePage.Name | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Type' -Value $wp.type | |
If ($null -ne $wp.title) { | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Title' -Value $wp.title.Trim() | |
} | |
else { | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Title' -Value '' | |
} | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Control Type' -Value $wp.controlType | |
$pageObject | Add-Member -MemberType NoteProperty -Name 'Order' -Value $wp.order | |
$webpartValues += $pageObject | |
} | |
} | |
} | |
### | |
$webpartValues | Export-Csv -Path $outputfilepath -NoTypeInformation | |
Write-Host 'Complete' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment