Skip to content

Instantly share code, notes, and snippets.

@garrytrinder
Created July 23, 2020 18:48
Show Gist options
  • Save garrytrinder/3718d21413c7fdff02a5c9db64fb729a to your computer and use it in GitHub Desktop.
Save garrytrinder/3718d21413c7fdff02a5c9db64fb729a to your computer and use it in GitHub Desktop.
ModernSharePointPageCopier
# FUNCTIONS
function Set-WelcomePage {
[CmdletBinding()]
param (
# Web Url that contains the page
[Parameter(Mandatory = $true)]
[System.Uri]
$WebUrl,
# Name of the page to set
[Parameter(Mandatory = $true)]
[String]
$PageName,
# SharePoint Access Token
[Parameter(Mandatory = $true)]
[String]
$AccessToken
)
process {
$headers = @{
"Authorization" = "Bearer $AccessToken"
"Accept" = "application/json;odata=verbose"
"Content-Type" = "application/json;odata=verbose"
"IF-MATCH" = "*"
"X-HTTP-METHOD" = "PATCH"
}
$body = @{
"__metadata" = @{
"type" = "SP.Folder"
}
"WelcomePage" = "SitePages/$PageName"
}
$response = Invoke-WebRequest -Method Post -Uri "$($WebUrl.AbsoluteUri)/_api/web/rootfolder" -Headers $headers -Body (ConvertTo-Json $body)
}
}
function Get-ClientPageContent {
[CmdletBinding()]
param (
# Web Url that contains the page
[Parameter(Mandatory = $true)]
[System.Uri]
$WebUrl,
# Name of the page to get
[Parameter(Mandatory = $true)]
[String]
$PageName,
# SharePoint Access Token
[Parameter(Mandatory = $true)]
[String]
$AccessToken
)
process {
$headers = @{
"Authorization" = "Bearer $AccessToken"
"Accept" = "application/json"
}
$response = Invoke-WebRequest -Method Get -Uri "$($WebUrl.AbsoluteUri)/_api/web/getfilebyserverrelativeurl('$($WebUrl.AbsolutePath)/SitePages/$PageName')/ListItemAllFields" -Headers $headers
$ListItemAllFields = $response.Content | ConvertFrom-Json -AsHashtable
@{
"CanvasContent" = $ListItemAllFields.CanvasContent1
"LayoutWebpartsContent" = $ListItemAllFields.LayoutWebpartsContent
}
}
}
function Set-ClientPageContent {
[CmdletBinding()]
param (
# The page
[Parameter(Mandatory = $true)]
[Hashtable]
$PageContent,
# Web Url that contains the page
[Parameter(Mandatory = $true)]
[System.Uri]
$WebUrl,
# Name of the page to set
[Parameter(Mandatory = $true)]
[String]
$PageName,
# SharePoint Access Token
[Parameter(Mandatory = $true)]
[String]
$AccessToken
)
process {
$WebUrl = [System.Uri]$WebUrl
$getHeaders = @{
"Authorization" = "Bearer $AccessToken"
"Accept" = "application/json"
}
$getResponse = Invoke-WebRequest -Method Get -Uri "$($WebUrl.AbsoluteUri)/_api/web/getfilebyserverrelativeurl('$($WebUrl.AbsolutePath)/SitePages/$PageName')/ListItemAllFields" -Headers $getHeaders
$getResponse.Content > output.json
$content = $getResponse.Content | ConvertFrom-Json -AsHashtable
$postHeaders = @{
"Authorization" = "Bearer $AccessToken"
"Accept" = "application/json;odata=verbose"
"Content-Type" = "application/json;odata=verbose"
"IF-MATCH" = "*"
"X-HTTP-METHOD" = "PATCH"
}
$postBody = @{
"__metadata" = @{
"type" = "SP.Data.SitePagesItem"
}
"CanvasContent1" = $PageContent.CanvasContent
"LayoutWebpartsContent" = $PageContent.LayoutWebpartsContent
}
$postBody = $postBody | ConvertTo-Json
$postResponse = Invoke-WebRequest -Method Post -Uri "$($WebUrl.AbsoluteUri)/_api/web/lists/getbytitle('Site Pages')/items($($content.ID))" -Headers $postHeaders -Body $postBody
}
}
$ContentConfig = @(
@{
DestinationPageName = "LandingPage.aspx"
DestinationWebUrl = [System.Uri]"https://cpsglobaldev.sharepoint.com/teams/GTSandpit"
DestinationPageTitle = "Landing Page"
DestinationPageLayoutType = "Home"
SourcePageName = "Welcome.aspx"
SourceWebUrl = [System.Uri]"https://cpsglobaldev.sharepoint.com"
IsHomePage = $true
}
@{
DestinationPageName = "ContentPage.aspx"
DestinationWebUrl = [System.Uri]"https://cpsglobaldev.sharepoint.com/teams/GTSandpit"
DestinationPageTitle = "Content Page"
DestinationPageLayoutType = "Article"
SourcePageName = "Sample-Copy.aspx"
SourceWebUrl = [System.Uri]"https://cpsglobaldev.sharepoint.com/teams/GTSandpit"
IsHomePage = $false
}
)
$token = o365 util accesstoken get --resource "https://cpsglobaldev.sharepoint.com" --new
$ContentConfig | ForEach-Object {
Write-Output "Checking for existing page $($_.DestinationPageName) in $($_.DestinationWebUrl) ..."
$page = o365 spo page get --name $_.DestinationPageName --webUrl $_.DestinationWebUrl
if ($page -notmatch "Error") {
Write-Output "Existing page found... removing..."
o365 spo page remove --name $_.DestinationPageName --webUrl $_.DestinationWebUrl --confirm
}
Write-Output "Creating page $($_.DestinationPageName) in $($_.DestinationWebUrl) ..."
o365 spo page add --name $_.DestinationPageName --webUrl $_.DestinationWebUrl --title $_.DestinationPageTitle --layoutType $_.DestinationPageLayoutType --publish
if ($_.IsHomePage) {
Write-Output "Setting $($_.DestinationPageName) as welcome page in $($_.DestinationWebUrl) ..."
Set-WelcomePage -PageName $_.DestinationPageName -WebUrl $_.DestinationWebUrl -AccessToken $token
}
Write-Output "Obtaining page content from $($_.SourcePageName) in $($_.SourceWebUrl) ..."
$pageContent = Get-ClientPageContent -PageName $_.SourcePageName -WebUrl $_.SourceWebUrl -AccessToken $token
Write-Output "Setting content for $($_.DestinationPageName) in $($_.DestinationWebUrl) with content from $($_.SourcePageName) in $($_.SourceWebUrl)..."
Set-ClientPageContent -PageContent $pageContent -PageName $_.DestinationPageName -WebUrl $_.DestinationWebUrl -AccessToken $token
Write-Output "Publishing page $($_.DestinationPageName) in $($_.DestinationWebUrl) ..."
o365 spo page set --name $_.DestinationPageName --webUrl $_.DestinationWebUrl --publish
Write-Output "Done"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment