Home > SharePoint 2010 > MySite Branding for SharePoint 2010

MySite Branding for SharePoint 2010

July 4th, 2011

randing of MySites is always a challenging task. It was complicated in SharePoint 2007 and still confusing in SharePoint 2010. Recently we had a requirement to applying cooperate branding for MySites. I tried to find a reasonable solution but nothing was fitting well in the required scenario. So after completion I decided to share my idea with the world.

Before start applying new branding for MySites. We need to understand that MySites are actually use two different types of site collections. First you need a MySite host site collection and then each personal site is a separate site collection. So we have to apply branding on both sites.

Now come to the main task. There could be different ways to customise the MySites. But the best possible solution would be using feature stapling. If you are new to feature stapler you can see this good post from Chris (http://www.sharepointnutsandbolts.com/2007/05/feature-stapling.html).

So we need to create first feature with Site level scope. This feature will be called MySiteStaplee. This feature will deploy master pages to the site and also change the master page for the site. Right first step would be to create a new project in Visual Studio 2010 and create a feature with Site level scope. It will look like this.

MySite Stapler Project View
Now we need to attach an elements.xml to include the master pages to deploy through this feature. Now it is bit different in Visual Studio 2010. We need to add a module. Add master page in the module, and make changes in the elements.xml file as below.

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”MasterPages” List=”116″ Url=”_catalogs/masterpage”>
<File Path=”MasterPages\salman.master” Url=”salman.master” Type=”GhostableInLibrary” />
</Module>
</Elements>

Your project will look like this now.

Now add a property to the master with a value as master name.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/“>
<Properties>
<Property Key=”MySiteMasterPage” Value=”/_catalogs/masterpage/salman.master”/>
</Properties>
</Feature>

Next thing we need to add a feature event receiver to replace the master page for mysite. For this add feature event receiver with the MySiteStaplee. Your code for featureactivated will look like this.

/// <summary>
/// Key for MySite master.
/// </summary>
private const string MySiteMasterPage = “MySiteMasterPage”;

/// <summary>
/// Set master page on feature activation.
/// </summary>
/// <param>The SPFeatureReceiverProperties object.</param>
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;

if (site != null)
{
SPWeb web = site.RootWeb;
string oldMasterUrl = web.MasterUrl;

web = site.RootWeb;
string newMySiteMasterUrl = properties.Feature.Properties[MySiteMasterPage].Value;
if (!string.IsNullOrEmpty(newMySiteMasterUrl))
{
web.CustomMasterUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, newMySiteMasterUrl);
web.MasterUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, newMySiteMasterUrl);
}

web.Update();
}
}

Ok, so your staple feature is ready. Now you need a stapler to staple your feature with mysite site definition.
In order to achieve this create a new project. Create a new feature in this project with Farm level scope. It will look like this.

Now we need to staple the MySiteStaple with MySite host and MySite personal site definitions. So we need elements file here. Add a new Empty Element from Visual Studio 2010. Remember mysite host site definition’s template name is SPSMSITEHOST and MySite personal site template name is SPSPERS. Your elements file will look like this.

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>
<!– Feature to staple with MySite host site collection –>
<FeatureSiteTemplateAssociation TemplateName=”SPSMSITEHOST#0″ >
</FeatureSiteTemplateAssociation>
<!– Feature to staple with MySite personal site collection –>
<FeatureSiteTemplateAssociation TemplateName=”SPSPERS#0″ >
</FeatureSiteTemplateAssociation>
</Elements>

Ok so all is done. Now you have to deploy these wsp solutions to the server. This will apply your master page with MySite host and MySite personal site definitions. So whenever you create a new MySite host or whenever a user creates his/her MySite, this feature will replace the master page for the new site. Your final solution structure will look like this in visual studio 2010.

I hope it will help you start the branding. Please wait for my next post with the advance solution required for some more branding issues with MySites.

Cheers.

Comments are closed.