-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplyTheme.ps1
96 lines (75 loc) · 2.8 KB
/
applyTheme.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
96
<#
.SYNOPSIS
Applies a Classic Theme for a modern site
.EXAMPLE
Apply theme
PS C:\> $Credentials = Get-Credential
PS C:\> .\applyTheme.ps1 -url "https://yourtenant.sharepoint.com/sites/yoursite" -Credentials $Credentials
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, HelpMessage="Enter the Modern site URL, e.g. 'https://yourtenant.sharepoint.com/sites/yoursite'")]
[String]
$url,
[Parameter(Mandatory = $false, HelpMessage="Optional administration credentials")]
[PSCredential]
$Credentials
)
#===================================================================================
# Get credentials if not passed as parameter
#===================================================================================
if($Credentials -eq $null)
{
$Credentials = Get-Credential -Message "Enter Admin Credentials"
}
# first upload files with PnP
# connect to SP Online
Write-Host -ForegroundColor Green "Connecting to SP Online"
Connect-PnPOnline $url -Credentials $Credentials
Write-Host
# Debug
Write-Host -ForegroundColor Green "Turning on debug mode"
Set-PnPTraceLog -On -Level Debug
Write-Host
Write-Host -ForegroundColor Green "Deploy custom Classic theme to Modern site"
# Upload the theme assets
Write-Host -ForegroundColor Green " -- Upload theme assets"
Apply-PnPProvisioningTemplate -Path .\files-theme.xml
Write-Host -ForegroundColor Cyan " ---- Theme assets upload complete"
# Now use CSOM to apply the theme
# connect
Write-Host -ForegroundColor Green "Connecting to client context"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$Context.Credentials = $Credentials
Write-Host
$web = Get-PnPWeb
Write-Host -ForegroundColor Green "Get-PnPWeb"
if($web)
{
Write-Host -ForegroundColor Green "Web exists"
try
{
$colorPaletteUrl = $web.ServerRelativeUrl + "/SiteAssets/test.spcolor"
$fontSchemeUrl = Out-Null
$backgroundImageUrl = Out-Null
$shareGenerated = $true;
Write-Host -ForegroundColor Yellow " -- Applying theme"
$web.ApplyTheme($colorPaletteUrl, $fontSchemeUrl, $backgroundImageUrl, $shareGenerated)
Write-Host -ForegroundColor Yellow " -- Load web"
$web.Context.Load($web)
Write-Host -ForegroundColor Yellow " -- Set high timeout"
$web.Context.RequestTimeout = [System.Threading.Timeout]::Infinite
Write-Host -ForegroundColor Yellow " -- Execute"
$web.Context.ExecuteQuery()
Write-Host "Theme Applied!"
}
catch
{
Write-Host -ForegroundColor Red "Exception occurred!"
Write-Host -ForegroundColor Red "Exception Type: $($_.Exception.GetType().FullName)"
Write-Host -ForegroundColor Red "Exception Message: $($_.Exception.Message)"
}
}
Write-Host
Write-Host -ForegroundColor Cyan "Site deployment complete!"