Posted by DevExpert on February 11th, 2009
I briefly went over my approach to creating a custom SharePoint theme in my previous post, and I even included a downloadable solution package that you can install on your farm (provided you have the .NET 3.5 Framework installed). How did I accomplish this? Pretty easily actually. Unfortunately there isn’t much documentation or examples of this out there, so allow me to put an end to that!
SharePoint features and solutions are absolutely essential if you want to provide an easy and maintainable method of deploying custom artifacts to your SharePoint servers. A theme is a perfect candidate for this, as everything is file-system based and located with the 12 directory. The only odd thing that throws a monkey wrench in this seemingly simple process is the SPTHEMES.XML file, which must be updated to include an entry for your custom theme. Here’s a portion of that file, with the custom theme I developed in my previous post highlighted:
Now, you could by all means include the SPTHEMES.XML file in your solution and have it overwrite the existing file, but what if you have other themes defined in your file? That approach will overwrite it, and you’ll have to reenter everything. The recommended approach to this is to create a feature receiver that fires when the feature is installed and modifies the SPTHEMES.XML. When the feature is installed, a custom <Templates> section is added for the custom theme, and when it’s uninstalled it’s removed.
NOTE: A feature is “installed” when you run the STSADM –o installfeature command, or when a feature is contained in a solution package and that solution package is deployed.
One important thing to mention is that this feature is scoped for the farm, not an individual site-collection or site, as the files deployed to the file system are used by the entire farm. Let’s first take a look at the feature.xml file:
As you can see, it is executing a custom FeatureReceiver class. At a high-level, the receiver looks like this:
I received a couple comments that let me know that if this feature is activated on a farm with multiple front-ends, then the FeatureActivated event only gets fired on a single web front-end. This is absolutely TRUE, and an oversight on my part (thanks guys!). The solution is extremely simple – put your code in the FeatureInstalled and FeatureUninstalling events instead, as these get fired on EVERY web front-end in your farm!! I’ve modified the code to reflect this:
For the sake of simplicity, I’m not including code to set the theme on any sites. Since this is a farm feature, it doesn’t make sense to set the theme for a particular site here, but by all means if you wanted to you could. Basically this feature receiver is only for modifying the SPTHEMES.XML file.
Let’s take a look at the ModifySPTheme() method. It uses LINQ to XML to open and parse the file, add the necessary elements when the feature is installed, and remove it when the feature is uninstalling:
Pretty slick, huh? Now, keep in mind that this only modifies the SPTHEMES.XML file – it does NOT set the theme for any site. It will still be up to the user or site admins to set the theme for a given site. Also, if this theme is already applied to a site and the feature is deactivated, it won’t reset the theme – it will only remove the entry from the theme settings page in Site Settings. Finally, if a site has this theme installed and the solution package is uninstalled and/or removed, your site’s theme will be messed up because the source files are gone. It is probably beneficial to at least loop through all the sites and reset the theme to something else if and when the feature is deactivated, but I’ll leave that to you. Enjoy!