Summary
This script will extract Space content type page as template file and saves it as new space page to SharePoint. Note, this script does not include referenced files e.g. images, videos etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$Url,
$saveDir = (Resolve-path ".\"),
[Parameter(Mandatory = $true)]
[string]$pageTitle,
[Parameter(Mandatory = $true)]
[string]$templateName
)
begin{
Write-Host $saveDir
Write-Host "Connecting to " $Url
Connect-PnPOnline -Url $Url -Interactive
}
process {
Write-Host "Getting page..."l
$web = Get-PnPWeb
$sourceSite = $web.ServerRelativeUrl
$library = "Site Pages"
#get all pages in the site pages library
$pages = Get-PnPListItem -List $library
#save current homepage
$currentHomePage = Get-PnPHomePage
$pagesList = New-Object System.Collections.Generic.List[System.Object]
$pages | Foreach-Object{
if($_.FileSystemObjectType -eq "File"){
$pagePath = $_.FieldValues["FileRef"]
if($_.FieldValues["Title"]) {$pageTitleLibrary = $_.FieldValues["Title"].ToString()}
#set space page as home page and save the template
if($pageTitleLibrary.ToLower() -eq $pageTitle.ToLower()){
Set-PnPHomePage -RootFolderRelativeUrl ($pagePath -replace ($sourceSite+"/"), "")
Get-PnPSiteTemplate -out $($saveDir.Path + "\" + $templateName + ".xml") -Handlers PageContents
}
}
}
#change back default homepage
Set-PnPHomePage -RootFolderRelativeUrl $currentHomePage
#wait for homepage to be set
Start-Sleep -Seconds 5
#open and update xml
$xmlFile = [xml][io.File]::ReadAllText($($saveDir.Path + "\" + $templateName + ".xml"))
$xmlFile.Provisioning.Templates.ProvisioningTemplate.RemoveChild($mainFile.Provisioning.Templates.ProvisioningTemplate.WebSettings)
$xmlfile.Provisioning.Templates.ProvisioningTemplate.ClientSidePages.ClientSidePage.Title = $("##PAGENAME##").ToString()
$xmlfile.Provisioning.Templates.ProvisioningTemplate.ClientSidePages.ClientSidePage.PageName = $("##PAGENAME##.aspx").ToString()
#save final template
$mainFile.Save($($saveDir.Path + "\" + $templateName + ".xml"))
((Get-Content -path $($saveDir.Path + "\" + $templateName + ".xml") -Raw) -replace '##PAGENAME##', $pageTitle) | Set-Content -Path 'temp.xml'
#create new page using template
Invoke-PnPSiteTemplate -path temp.xml
Remove-Item -Path 'temp.xml'
Write-Host "Space file added" -ForegroundColor Cyan
}