Office 365 – PowerShell script to add / create custom theme to our Tenant
Hi All,
Today, I got an opportunity to write PowerShell script to add our custom theme to the Tenant. So thought to be share with all of us.
I Put detailed comments, tried to be self explanatory.
<#
.SYNOPSIS
Ensures that the our theme is added to tenant
.PARAMETER CredentialFilePath
Office 365 system account credential file path having two lines in following format
UserName
Password
.PARAMETER TenantAdminURL
Office 365 tenant admin site URL
.PARAMETER ThemeJSONFilePath
Path of theme.json file
.PARAMETER ThemeName
Name of Theme
#>
param
(
[parameter(Mandatory=$true)][string]$CredentialFilePath,
[parameter(Mandatory=$true)][string]$TenantAdminURL,
[parameter(Mandatory=$true)][string]$ThemeJSONFilePath,
[parameter(Mandatory=$true)][string]$ThemeName
)
if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
#Get the user credential file path and getting user from it
$user = Get-Content $CredentialFilePath | Select-Object -First 1
#Getting password
$password = Get-Content $CredentialFilePath | Select-Object -First 1 -Skip 1
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
#Connect to Office 365
$spoManagementCred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $user, $securePassword
Connect-SPOService -Url $TenantAdminURL -Credential $spoManagementCred
#Get the theme JSON
$json = Get-Content $ThemeJSONFilePath -Raw -ErrorAction Stop
$jsonObj = $json | ConvertFrom-Json
$theme = @{}
foreach ($property in $jsonObj.PSObject.Properties) {
$theme[$property.Name] = $property.Value
}
#Add theme to tenant
Add-SPOTheme -Name $ThemeName -Palette $theme -IsInverted $false -ErrorAction Stop
#Fetch the newly added theme to check if it is added successfully or not
$getTheme = Get-SPOTheme -Name $ThemeName
if($getTheme -ne $null)
{
#Theme added successfully
write-host "Theme created successfully."
}
else
{
#Theme not added successfully.
write-host "Theme is not created successfully. Please check the parameters and try again"
}
References:
SharePoint site theming: PowerShell cmdlets
Get-SPOTheme
Add-SPOTheme
Thanks for Reading 🙂
Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.
Stay tuned on Knowledge-Junction, will come up with more such articles.
