I've moved my blog to http://blog.falchionconsulting.com!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Monday, January 28, 2008

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:

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

14 comments:

Andy Burns
said...

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

Not sure I understand - are you saying this is out-of-box behaviour, and are you sure about this? I've just tried it, and that wasn't what I saw happen. Site themes appear to be independent - even for the root site of a root site collection.

Do you have another feature installed? Some funky Site Definition maybe? Or, it just hit me - are you using the Alternate CSS URL on the master page configuration page?

Or are you saying that you can do that with the applytheme command? I looked at the code, but didn't see that.

What would be good would be to be able to:

- Set the theme for a site and all subsites (what you've already written)- Replace a theme on a site and all subsites with another theme (i.e. swap Theme A for Theme B, although there might be some sites in the tree using Theme C)- 'Refresh' a theme (which might just be replacing Theme A with Theme A). This would be good for solving the problem here - http://www.heathersolomon.com/blog/archive/2008/01/30/SharePoint-2007-Design-Tip-Import-your-CSS-for-SharePoint-Themes.aspx

Very neat idea though - I was thinking about writing something similar (except I'd put it into the Site Admin pages).

Would be happy to help by writing some of this - your code is straight forward enough. Email andy at the domain novolocus.com

I'd have to look at it again but the behavior I described was what I witnessed - changing the theme of the root web (site collection) resulted in all sub-sites using the specified theme - if I set the theme for a specific sub-site then changing it at the root level did not affect the sub-site.

It probably wouldn't take much to modify the code to support the "refresh" ability you suggest - just a matter of deleting the "_theme" folder (may not be necessary) and re-setting the theme (the same ability can basically solve the "replace" ability you mention).

After applying the theme using this extension, when I go to http://[sitepath]/_layouts/themeweb.aspx, the applied theme is not highlighted. Is there some additional setting required to make this right? Not a big deal, but just for completeness...

That error occurs when the dll cannot be found (i.e., it's not in the GAC). I'm assuming that you replaced "gl-" with "ss-" which is fine and shouldn't affect anything. Make sure the dll is GAC'd and you should okay.

Great post and description on how to Apply a Theme via the STSADM extensions you have developed.

In order to add a command to your list, could you show us how to do the same for a custom master page? Basically, same need, apply the custom master page to the Site Collection (top level) and Sub-Sites. In addition, when a new sub-site is created, for that sub-site to inherit the Custom Master Page from the Site Collection (top level).

In it you'll find everything you need to write your own code to do all of what you ask (it's much better to do this as a feature receiver as described but you could modify the code to run via an stsadm command).

Gary, Once again, this does not seem to apply to all the site collections in the applications even with recurse flag turned on. Is it only for sub webs underneatht the root or should it work for all webs in the application.

Sparky - like the other commands you've commented on, this one too only works for one site collection at a time. You'll need to combine with powershell if you want to do more. Any commands that I have that operate on more than one site collection will have a scope parameter where you can specify web application, site collection, or web.

Thanks. I have never used powershell. However I plan to download and try it. Would it be simple to make this gl-applytheme apply at an application level through powershell. Just wanted to make sure I go through the exercise and attempt something you have not published through this command.

My Custom Commands & Cmdlets

Use of any tools included in the various downloads found at this site is at your own risk. Gary Lapointe cannot be held liable for any damage done to your environment through the use of any code or downloadable tools found on this site.

You may not repackage or sell any of the downloadable tools or associated source code. Downloading of the various tools implies that you acknowledge these restrictions.