Adding a custom theme through a feature

This feature is a farm level feature. It is very simple to do. It is comprised of the theme, including all images and CSS, feature.xml and elements.xml and a .cs file that does the dynamic adding to SPTHEMES.xml, which is necessary for the theme to show up.


<?xml version=”1.0″ encoding=”utf-8″?>
<Feature xmlns=””
Title=”Custom Belltown”
Description=”Custom Belltown Theme”
ReceiverAssembly=”NEW_BELLTOWN_THEME, Version=,
Culture=neutral, PublicKeyToken=54d93db7bee0a736″
ReceiverClass =”NEW_BELLTOWN_THEME.FeatureCode.CustomTheme”>

<ElementManifest Location=”elements.xml”/>

elements.xml //in this case it’s actually an empty file

Add the theme to 12/template/themes/custombelltown/<ALL ASSETS>


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Reflection;
using System.Xml.Linq;
using System.IO;
using System.Linq;

namespace NEW_BELLTOWN_THEME.FeatureCode
class CustomTheme : SPFeatureReceiver
private enum ModificationType { Add, Remove }

public override void FeatureInstalled(SPFeatureReceiverProperties properties)

// if necessary, loop through all sites and set theme

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)

// if necessary, loop through all sites and reset theme

public override void FeatureActivated(SPFeatureReceiverProperties properties)
// do nothing

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
// do nothing

private void ModifySPTheme(ModificationType type)
XDocument doc = null;
XNamespace ns = “”;

// path to the SPTHEMES.XML file
string spthemePath = Path.Combine(SPUtility.GetGenericSetupPath(@”TEMPLATE\LAYOUTS\1033″), “SPTHEMES.XML”);
string contents = string.Empty;

// read the contents of the SPTHEMES.XML file
using (StreamReader streamReader = new StreamReader(spthemePath))
contents = streamReader.ReadToEnd();

using (StringReader stringReader = new StringReader(contents.Trim()))
// create a new XDocument from the contents of the file
doc = XDocument.Load(stringReader);

// retrieve all elements with a TemplateID of ‘VISTA’.  At most, there should only be one
var element = from b in doc.Element(ns + “SPThemes”).Elements(ns + “Templates”)
where b.Element(ns + “TemplateID”).Value == “CustomBelltown”
select b;

// determine if the VISTA theme element already exists
bool exists = (element != null && element.Count() > 0);

if (type == ModificationType.Add)
if (!exists)
// create an XElement that defines our custom VISTA theme
XElement xml =
new XElement(ns + “Templates”,
new XElement(ns + “TemplateID”, “CustomBelltown”),
new XElement(ns + “DisplayName”, “Custom Belltown”),
new XElement(ns + “Description”, “A custom belltown theme”),
new XElement(ns + “Thumbnail”, “images/thbelltown.gif”),
new XElement(ns + “Preview”, “images/thbelltown.gif”));

// add the element to the file and save
doc.Element(ns + “SPThemes”).Add(xml);
if (exists)
// if the element exists, remove it and save



Comments are closed.