Monday, July 2, 2012

SharePoint sub site branding fix

Wanted to blog this long back - but :)
At times, SharePoint is very weird. Here's a situation. Client have customer master page , he applied to Root Site and also check the option to apply it to all the sub-sites. All good so far.
No what if client create a new sub-site in future.

Created a new feature to bridge the gap and also creates a new feature to ensure that this feature get activated on all the newly created sub-sites.

Steps / Code:
1. Create a new feature ( Branding_Sync / Scope = Web )
2. Add a feature receiver with following code on activation

public override void FeatureActivated(SPFeatureReceiverProperties properties)
            SPWeb web = properties.Feature.Parent as SPWeb;

            if (!web.IsRootWeb)
                Hashtable hash = web.AllProperties;
                web.MasterUrl = web.ParentWeb.MasterUrl;
                hash["__InheritsMasterUrl"] = "True";
                web.CustomMasterUrl = web.ParentWeb.CustomMasterUrl;
                web.SiteLogoUrl = web.ParentWeb.SiteLogoUrl;
                hash["__InheritsCustomMasterUrl"] = "True";
3. Create a new feature ( Feature2 / Scope = Web Application )
4. Add a new "Empty Element" manifest and paste following lines
5. Replace the "FeatureGUID" with the guid of Feature1 above
lt;?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="STS#0" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="STS#1" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="STS#2" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="WIKI#0" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="BLOG#0" />

  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="MPS#0" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="MPS#1" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="MPS#2" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="MPS#3" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="MPS#4" />

  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="SRCHCENTERLITE#0" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="SRCHCENTERLITE#1" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="BDR#0" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="OFFILE#0" />
  <FeatureSiteTemplateAssociation Id="FeatureGUID" TemplateName="SPSREPORTCENTER#0" />