SharePoint Online: How to Change Site Theme using PowerShell?

In SharePoint Online you can customize the look and feel of your site by changing themes to reflect your corporate branding. A theme in SharePoint Online defines the colors, fonts, image, and page layout for the SharePoint sites. SharePoint provides a default set of site themes,  and we also have the ability to create and deploy our custom themes as well.

How to Change the Theme in SharePoint Online?
To change a theme for a site, do the following:
  • Navigate to the SharePoint Online site where you want to change the theme >> On the site’s home page, Click on Settings gear >> Choose "Change the look" from the Site Settings menu.
    sharepoint online powershell change theme
  • On the Change the Look page, select the theme to use as the starting point for the new site layout. (You can also go to Site Settings >> Click on "Change the Look" link from the Look and Feel section)
    sharepoint online subsite inherit theme
  • On the Change the Look configuration page, you can further customize the theme to meet your requirements, such as:
    • You can change the background image (or remove).
    • The color scheme for the theme can be further changed by using the colors drop-down and select the desired color combination.
    • The site master page layout can be changed to two available page layouts: Oslo and Seattle.
    •  The font family for use in the site can be selected. Select the font listing from the Fonts drop-down list.
  • Once any changes to the theme’s background image, colors, site layout, and fonts have been made, the theme can be tested by clicking the "Try It Out" link. Clicking this link will generate a preview of the look. You see the layout as it will appear within the site.
    sharepoint online apply theme to subsites
  • In the Preview page, decide whether the look is complete. Clicking the "No, Not Quite There" link returns you to the Change the Look configuration page, where you can customize the look and feel. Clicking the "Yes, Keep It" link changes the current theme of the site.
    sharepoint theme apply all sites
You can reset the theme of a SharePoint Online site by switching to the "Office" theme! Go to Site settings >> Look and feel >> Change the look. >> Select the "Office" theme and apply. This gets you the default Microsoft look. Any user with "Design" or "Full Control" permission level can change site themes! Here is my other post on change theme using PowerShell in SharePoint On-premises: How to Apply Theme using PowerShell in SharePoint?

The Theme Generator tool is available Online at: https://aka.ms/themedesigner

Themes Inheritance in Hub-Sites:
Themes are inherited across all sites associated with a hub! If you attempt to configure the look and feel of a site associated with a hub you'll be notified with "Your site is connected to the <Hub-site name> hub site and is set to automatically adopt the same theme." message. So, In order to change the theme for the associated sites, you'll need to go to the hub site and set theme options there!
Your site is connected to the  hub site and is set to automatically adopt the same theme

SharePoint Online: Apply theme using PowerShell
To change the SharePoint theme programmatically, we need Theme Url, Image Url, Font Scheme URL of templates from the Composed Looks list. You can get them by navigating through "/_catalogs/design/AllItems.aspx" URL of your site collection.
#Import PowerShell Module for SharePoint Online
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
    
#Parameters for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$ColorPaletteUrl = "/sites/marketing/_catalogs/theme/15/palette002.spcolor"
$FontSchemeUrl =  "/sites/marketing/_catalogs/theme/15/fontscheme002.spfont"
$BackgroundImageUrl = "/_layouts/15/images/image_bg002.jpg"

#Get Credentials to connect
$Cred= Get-Credential
  
Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()
    
    #Apply Theme
    $Web.ApplyTheme($ColorPaletteUrl,$FontSchemeUrl,$BackgroundImageUrl,$True)
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error Changing Theme: $($_.Exception.Message)" -foregroundcolor Red
}
If you want to skip Font scheme or background image parameters, Set those variables to Out-Null. E.g. $BackgroundImageUrl = Out-Null.

PowerShell to Apply Theme to Subsites in SharePoint Online:
Let's change the theme for all sites in a site collection in SharePoint Online.
#Import PoweShell Module for SharePoint Online
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

#Parameters for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
#Theme URLs - Located only at Root Site
$ColorPaletteUrl = "/sites/marketing/_catalogs/theme/15/palette001.spcolor"
$FontSchemeUrl =  "/sites/marketing/_catalogs/theme/15/fontscheme001.spfont"
$BackgroundImageUrl = "_layouts/15/images/image_bg001.jpg"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get the Root web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()

    #Function to change the site theme
    Function Set-SPOSiteTheme($Web)
    {
        #Apply Theme
        $Web.ApplyTheme($ColorPaletteUrl,$FontSchemeUrl,$BackgroundImageUrl,$True)
        $Ctx.ExecuteQuery()
        Write-host -f Green "Theme Applied on site '$($Web.Url)' Successfully!"

        #Process each subsite in the site
        $Subsites = $Web.Webs
        $Ctx.Load($Subsites)
        $Ctx.ExecuteQuery()        
        Foreach ($SubSite in $Subsites)
        {
            #Call the function Recursively
            Set-SPOSiteTheme($Subsite)
        }
    }
    #Call the function to change the theme of the web
    Set-SPOSiteTheme($Web)
}
catch {
    write-host "Error Changing Theme: $($_.Exception.Message)" -foregroundcolor Red
}

PnP PowerShell to Set Theme in SharePoint Online 
Themes can be applied with Set-PnPTheme cmdlet in SharePoint Online.
#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credential (Get-Credential) #-UseWebLogin

#Change Set Theme
Set-PnPTheme -ColorPaletteUrl "/_catalogs/theme/15/palette008.spcolor" -BackgroundImageUrl "/_layouts/15/images/image_bg008.jpg" -FontSchemeUrl "/_catalogs/theme/15/fontscheme003.spfont"

Inherit Theme from the Parent Site using PnP PowerShell
We can inherit the theme and apply it to subsites using -ResetSubwebsToInherit switch. Here is how:
Set-PnPTheme -ColorPaletteUrl "/_catalogs/theme/15/palette008.spcolor" -BackgroundImageUrl "/_layouts/15/images/image_bg008.jpg" -FontSchemeUrl "/_catalogs/theme/15/fontscheme003.spfont" -ResetSubwebsToInherit
This makes subsites to inherit theme!

Remove Theme from Site (Reset Site Theme to Default):
You can reset themes by calling the cmdlet "Set-PnPTheme" without any parameters, which removes the theme from the site!

Related posts:

1 comment:

  1. Thank you so much dear! It works just amazing!!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.