-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-SPOPageTemplateXML.ps1
95 lines (67 loc) · 3.73 KB
/
Invoke-SPOPageTemplateXML.ps1
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
param($tenantUrl, $siteUrlSlug, $templatePath=".\template\template.xml")
# $tenantUrl: the base url of your tenant site i.e. replace <tenant>
# $siteUrlSlug: url slug to follow the tenant site to the site you wish to apply the template to, i.e https://<tenant>.sharepoint.com<slug>, where slug may equal '/sites/siteName'; replace <site>
# templatePath: ".\template\template.xml" #path to the template file you wish to apply
$fullSiteUrl = $tenantUrl + $siteUrlSlug
#relative url paths to branding files below. i.e. where the handlers should expect to find the files on the target site.
$headerLogo = $siteUrlSlug + '/SiteAssets/__sitelogo__headerLogo.png'
$headerBackground = $siteUrlSlug + '/SiteAssets/__extendedHeaderBackgroundImage__headerBanner.png'
$footerLogo = $siteUrlSlug + '/SiteAssets/__footerlogo__footerLogo.png'
if ($tenantUrl -and $siteUrlSlug -and $templatePath){
Write-Host @"
Beginning SPO site template export script.
Requesting connection to tenant.
"@
Connect-PnPOnline -Url $fullSiteUrl -Interactive
$web = Get-PnPWeb -Includes WebTemplate
$targetSiteTemplateType = $web.WebTemplate.Trim()
Write-Host "Successfully connected to tenant. Connected to site: ", $web.Title, "."
Switch ($targetSiteTemplateType){
'GROUP' {Write-Host "Target site is of Teams site type."}
'SITEPAGEPUBLISHING' {Write-Host "Target site is of Communication site type."}
default {Write-Host "Target site is of type ", $siteTemplateType, "."}
}
try {
$xml = [xml](Get-Content $templatePath)
$siteTemplateType = $xml.Provisioning.Templates.ProvisioningTemplate.BaseSiteTemplate
$siteTemplateType = $siteTemplateType -replace "#.*" #remove #0 etc from string for comparison between site template types
Switch ($siteTemplateType){
'GROUP' {Write-Host "Local template site is of Teams site type."}
'SITEPAGEPUBLISHING' {Write-Host "Local template site is of Communication site type."}
default {Write-Host "Local template site is of type ", $siteTemplateType, "."}
}
} catch {
Write-Host "Unable to extract site template property from XML site template."
Write-Host "Continuing to attempt to apply template, though, issues may arise..."
}
if($siteTemplateType -eq $targetSiteTemplateType){
Write-Host "Site types of template and target site match."
} else {
Write-Host "Site types of template and taget site do not match. Template application may not be entirely successful."
}
Write-Host "Applying template to ", $web.Title, "."
Invoke-PnPSiteTemplate -Path $templatePath
Write-Host "Updating site theme."
Set-PnPWebTheme -Theme "Therapy Focus Theme"
Write-Host "Updating site header logo."
if($siteTemplateType -eq "GROUP"){
Set-PnPWebHeader -SiteLogoUrl $footerLogo
} else {
Set-PnPWebHeader -SiteLogoUrl $headerLogo
}
Write-Host "Updating site header display setting and background."
if($siteTemplateType -eq "GROUP"){
Set-PnPWebHeader -HeaderLayout Standard -HeaderEmphasis Strong -LogoAlignment Left
} else {
Set-PnPWebHeader -HeaderLayout Extended -HeaderBackgroundImageUrl $headerBackground -HeaderEmphasis Strong -LogoAlignment Left
}
Write-Host "Updating site footer logo, text and display settings if required."
if(!($siteTemplateType -eq "GROUP")){
Set-PnPFooter -LogoUrl $footerLogo -Title "Therapy Focus ©" -Enabled:$true -Layout Simple -BackgroundTheme Strong
}
Write-Host "Successfully applied site template."
Write-Host "Disconnection from tenant."
Disconnect-PnPOnline
} else {
Write-Error "Missing tenantURL or SiteURL"
}