[SharePoint 2013] Using PowerShell to Set the Theme Color Palette of a SharePoint Site

SharePoint 2013 includes a pretty useful theming engine that is light years ahead of previous versions of SharePoint. There is one caveat that is a bit annoying: you cannot apply the themes to certain types of sites through the web interface. For instance, you cannot set the theme color of the My Site host.

However, you can do this programmatically and through PowerShell. This example shows how to set the SharePoint theme palette for the My Site host:

$web = Get-SPWeb http://site/my
$file = $web.GetFile($web.ServerRelativeUrl + "/_catalogs/theme/15/Palette015.spcolor")
$theme = [Microsoft.SharePoint.Utilities.SPTheme]::Open("MySiteTheme", $file)
$theme.ApplyTo($web, $false)

Notes about this example:

  • “http://site/my” is the URL to the SharePoint site (SPWeb) that we are setting the theme for.

  • “/_catalogs/theme/15/Palette015.spcolor” is the color palette that is already on the site. You can add your own color palettes as well.

  • “MySiteTheme” is the name this generated theme takes on. You can put just about anything here.

  • On the $theme.ApplyTo method, the second parameter indicates whether or not this theme should propagate to each subsite. In this example, it is set to $false.

This script could be applied to sites that you cannot access this setting through the web interface or could even be applied to batches of sites. For instance, you could do a foreach statement to apply this to each SPWeb in a site collection.

Hopefully you find this useful.

Update:

Here is an example of using this to update the theme applied to each users personal site (My Content / SkyDrive):

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({ foreach ($site in (Get-SPSite http://site/my/personal/* -Limit ALL)) { ([Microsoft.SharePoint.Utilities.SPTheme]::Open("MySiteTheme", $site.RootWeb.GetFile($site.RootWeb.ServerRelativeUrl + "/_catalogs/theme/15/Palette015.spcolor"))).ApplyTo($site.RootWeb, $true) } })

John Chapman

Hello, I'm John Chapman. I am a SharePoint Developer for Sitrion (formerly NewsGator) living in Denver, Colorado. I develop solutions using SharePoint and .NET, and I thrive on the challenge of writing code to overcome the impossible, annoying, or otherwise difficult obstacles.

More Posts - Website - Twitter - LinkedIn - Google Plus

  • JohnC

    I got an exception on the line
    $theme.ApplyTo($web, $false)

    Exception calling “ApplyTo” with “2″ argument(s): “Unable to cast COM object of type ‘Microsoft.SharePoint.Library.SPRequestInternalClass’ to interface type ‘Microsoft.SharePoint.Library.ISPRequest’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{BDEADF28-C265-11D0-BCED-00A0C90AB50F}’ failed due to the following error: Bad variable type. (Exception from HRESULT: 0×80020008 (DISP_E_BADVARTYPE)).”

    I didn’t look into it further as I switched to a different approach as outlined here, http://jespermchristensen.wordpress.com/2013/08/02/changing-the-colors-of-your-sharepoint-2013-or-office-365-my-site/. This worked for me.