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:

   1: internal 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: 
  10: internal 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:  }
  29: }
Advertisements