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 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
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

SharePoint Online: Apply theme using PowerShell
#Import PoweShell 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 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 theme and apply 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!  You can reset themes by calling the cmdlet "Set-PnPTheme" without any parameters, which removes the theme from site!

To add a custom theme in SharePoint Online, use: SharePoint Online: Add Custom Theme using PowerShell
SharePoint Online: How to Change Site Theme using PowerShell? SharePoint Online: How to Change Site Theme using PowerShell? Reviewed by Salaudeen Rajack on March 02, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.