Apply Theme To A Classic SharePoint Online Site From An Existing Site Using SharePoint PnP PowerShell

In this blog, I will demonstrate how to get the theme of the Classic SharePoint Online site and apply the same theme to another Classic SharePoint Online Site using SharePoint PnP PowerShell script.

Pre-requisites (Environment Details)

  • Windows PowerShell
  • SharePointPnpPowerShellOnline Module

Please install the SharePointPnpPowerShellOnline module if it’s not already present using the below command.

  1. Install-Module SharePointPnPPowerShellOnline  

Source SharePoint Classic site with Red Theme

Destination SharePoint Classic site with Blue Theme

Variables Explanations in this Articles

  • Your Service Account Name
    $O365ServiceAccount="[email protected]"
  • Your Service Account Password
    $O365ServiceAccountPwd="Test123$"
  • Change Source SharePoint Site URL
    $SourceSharePointSiteURL="https://abc.sharepoint.com/sites/SharePointDocumentationCentre/teamsiteplayground"
  • Change Destination SharePoint Site URL
    $DestinationSharePointSiteURL="https://abc.sharepoint.com/sites/SharePointDocumentationCentre/DestinationSite"

Here you can see we have provided the password in plain text which you need to provide if you want this PowerShell script to run automatically through Timer Job or Scheduler .

For Manual Execution Please use Get-Credential command to read the User name and Password from the user input

Get Theme from Source SharePoint classic site

We have to get the theme of the Source SharePoint Classic site using the Get-PnPTheme command of PowerShell as a code snippet below.

#Connecting to Source Site to get the current theme    
Connect-PnPOnline -Url $SourceSharePointSiteURL -Credentials $PSCredentials    
#Getting the theme of the site    
$Theme=Get-PnPTheme -WarningAction Ignore    
Disconnect-PnPOnline    
#Disconnect source site   

First we have to connect the Source site using Connect-PnPOnline then get the theme of the site and store in the variable $Theme. Once we have stored the theme in the variable disconnect the site using Disconnect-PnPOnline command.

Set Theme to Destination SharePoint classic site

We have to set the theme to the Destination SharePoint classic site using the Set-PnPTheme as a code snippet below

#Connecting to Destination site to Apply theme  
Connect - PnPOnline - Url $DestinationSharePointSiteURL - Credentials $PSCredentials  
if ($Theme.Theme) {  
    #Apply the theme to the site  
    Set - PnPTheme - ColorPaletteUrl $Theme.Theme - ErrorAction SilentlyContinue  
}  
if ($Theme.ColorFile) {  
    #Apply the theme to the site  
    Set - PnPTheme - ColorPaletteUrl $Theme.ColorFile - ErrorAction SilentlyContinue  
}  
Disconnect - PnPOnline  
#Disconnect destination site

The first step is to connect to the destination SharePoint classic site . if $Theme variable has ColorFile or Theme property, set the theme using Set-PnPTheme command.

In some sites I have observed $Theme.Theme is null so in that case we have to use $Theme.ColorFile as ColorPaletteUrl to set the theme of SharePoint Classic Site.

Complete PowerShell Script

#Powershell script to Get a Theme of a site and set the same theme to another SharePoint Online Classic site.  
#Created By: Vinit Kumar  
# Variable - Change the parameter as it need  
$O365ServiceAccount = "[email protected]"  
$O365ServiceAccountPwd = "Test123$"  
$SourceSharePointSiteURL = "https://abc.sharepoint.com/sites/SharePointDocumentationCentre/teamsiteplayground"  
# Change Source SharePoint Site URL  
$DestinationSharePointSiteURL = "https://abc.sharepoint.com/sites/SharePointDocumentationCentre/DestinationSite"  
# Change Destination SharePoint Site URL  
#Ends  
# Get credentials to connect sharepoint online[SecureString] $SecurePass = ConvertTo - SecureString $O365ServiceAccountPwd - AsPlainText - Force[System.Management.Automation.PSCredential] $PSCredentials = New - Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)  
#Ends  
#Connecting to Source Site to get the current theme  
Connect - PnPOnline - Url $SourceSharePointSiteURL - Credentials $PSCredentials  
#Getting the theme of the site  
$Theme = Get - PnPTheme - WarningAction Ignore  
Disconnect - PnPOnline  
#Disconnect source site  
#Connecting to Destination site to Apply theme  
Connect - PnPOnline - Url $DestinationSharePointSiteURL - Credentials $PSCredentials  
if ($Theme.Theme) {  
    #Apply the theme to the site  
    Set - PnPTheme - ColorPaletteUrl $Theme.Theme - ErrorAction SilentlyContinue  
}  
if ($Theme.ColorFile) {  
    #Apply the theme to the site  
    Set - PnPTheme - ColorPaletteUrl $Theme.ColorFile - ErrorAction SilentlyContinue  
}  
Disconnect - PnPOnline  
#Disconnect destination site  

Destination SharePoint Classic site with Red Theme  after Executing the Script

In this blog, we have talked about how to get a color theme of Source SharePoint Online Classic site and apply to a destination SharePoint online Classic site using SharePoint PnP PowerShell. This blog will be useful in scenarios where we need to create a new SharePoint site and apply an existing Corporate Site theme using PowerShell.