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.

FEATURE.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/”
Id=”GUID”
Title=”Custom Belltown”
Description=”Custom Belltown Theme”
Scope=”Farm”
Version=”1.0.0.0″
ImageUrl=””
ReceiverAssembly=”NEW_BELLTOWN_THEME, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=54d93db7bee0a736″
ReceiverClass =”NEW_BELLTOWN_THEME.FeatureCode.CustomTheme”>

<ElementManifests>
<ElementManifest Location=”elements.xml”/>
</ElementManifests>
</Feature>

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

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

customtheme.cs

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)
{
ModifySPTheme(ModificationType.Add);

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

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
ModifySPTheme(ModificationType.Remove);

// 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 = “http://tempuri.org/SPThemes.xsd”;

// 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();
streamReader.Close();
}

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);
doc.Save(spthemePath);
}
}
else
{
if (exists)
{
// if the element exists, remove it and save
element.Remove();
doc.Save(spthemePath);
}
}

stringReader.Close();
}
}

}
}

Comments are closed.