This command came about as a result of wanting to be able to reset the theme of a web site when it was upgraded. I had created this code as part of the upgrade2
command and figured that I might as well pull it out into its own command so that it could be used independently of the upgrade
command. I called this command gl-applytheme
. Setting the theme is really simple – you just call the SPWeb
object’s ApplyTheme()
method and pass in the theme ID. The theme ID for OTB themes can be found in the SPThemes.xml file located in the ..\12\Layouts\Templates\[LCID]
folder. The core code can be seen below:
1internal static void ApplyThemeToWeb(string theme, string url, bool recurse)
2{
3 using (SPSite site = new SPSite(url))
4 using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
5 {
6 ApplyThemeToWeb(theme, web, recurse);
7 }
8}
9
10internal static void ApplyThemeToWeb(string theme, SPWeb web, bool recurse)
11{
12 web.ApplyTheme(theme);
13
14 if (recurse)
15 {
16 foreach (SPWeb subWeb in web.Webs)
17 {
18 try
19 {
20 ApplyThemeToWeb(theme, subWeb, recurse);
21 }
22 finally
23 {
24 subWeb.Dispose();
25 }
26 }
27 }
28}
The syntax of the command can be seen below:
C:\>stsadm -help gl-applytheme
stsadm -o gl-applytheme
Applies the specified theme to the specified site.
Parameters:
-url <url of the web to update>
-theme <id of the theme to apply (see C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Layouts\1033\SPThemes.xml for template IDs>
[-recurse (applies change to all sub-webs)]
Here’s an example of how to set the theme for a web to it’s default:
stsadm -o gl-applytheme -url "http://intranet" -theme none -recurse
Note that sub-sites of a site collection will use the theme specified for the root site collection unless explicitly set. This means that you can set the theme for an entire site collection by just setting the root without setting the sub-sites. If you pass in the recurse flag then it will set the theme for each sub-site explicitly which means that future changes to the root web’s theme will not affect the theme for sub-sites (this may or may not be what you want so make sure that you understand how this works).