AWG Blogs

Monday, March 12, 2012

Setting Theme in Feature Receiver

When provisioning web sites, the theme was not taking. To get it working I made the following change to RestoreWebProperties, which was generated by SharePoint Solution Generator.

string key = xWebProperty.Attributes["Key"].Value;
string value = xWebProperty.Attributes["Value"].Value;
//this does not work for __IncludeSubSitesInNavigation, use AllProperties instead
/* if (web.Properties.ContainsKey(key))
{
web.Properties[key] = value;
}
else
{
web.Properties.Add(key, value);
} */
//need explicit set, apparently setting via property bag does not effect update
if (key == "vti_themedefault")
{
try
{
web.ApplyTheme(value);
}
catch { }

}
else
{
web.AllProperties[key] = value;

}



Note, get the theme key from TemplateID element in SPTHEMES.XML. Also do web.Update() instead of web.Properties.Update().