Custom mysite masterpage for Personal sites

I just had to write down some notes on the steps needed to take when you want to set your own custom master page for every personal site.

If you don’t have the complete overview on how mysites and personal sites works together you should read this first: Sharepoint 2010 Mysite in detail

Since personal sites are in fact sitecollections (one per user) and masterpage settings are scoped to sitecollection you need feature staplers to have every new site use your own custom master page instead of the one default (v4.master)
You should furhtermore know at personal sites are created on the fly from the Mysite host when clicking “My Content” if you don’t already have one.

So here are the steps.

1. Create your own custom masterpage (ofcourse 🙂 and wrap it into a feature to make it appear to the Masterpage gallary for new sitecollections.

2. Create a feature with a feature receiver for setting the master page

The code for the feature receiver could look like this

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWeb web = ((SPSite)properties.Feature.Parent).RootWeb;
                web.MasterUrl = web.MasterUrl.Substring(0, web.MasterUrl.LastIndexOf('/') + 1) + "MyOwnCustom.master";
                web.CustomMasterUrl = web.CustomMasterUrl.Substring(0, web.CustomMasterUrl.LastIndexOf('/') + 1) + "MyOwnCustom.master";
                web.Update();
            }
            catch (Exception ex)
            {
                //Write error to ULS (Unified Logging System - 14/Logs) if desired (Remember to specify: using Microsoft.Office.Server.Diagnostics;)
                //PortalLog.LogString("MyOwnCustom master- Feature receiver: Set Master for new Personal sites - Error: " + ex.ToString());
            }
        }

The scope of this feature should be Site (Sitecollection)

3. Create another feature (the stapling feature)
This feature is used to tell Sharepoint which feature should be actived when a site is created using a certain sitedefinition.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <FeatureSiteTemplateAssociation Id="A392DA98-270B-4e85-9769-04C0FDE267AA" TemplateName="SPSPERS#0" />
  <!-- Publishing Prerequisites -->
  <FeatureSiteTemplateAssociation Id="AEBC918D-B20F-4A11-A1DB-9Ed84D79C87E" TemplateName="SPSPERS#0" />
  <!-- Publishing Resources -->
  <FeatureSiteTemplateAssociation Id="F6924D36-2FA8-4f0b-B16D-06B7250180FA" TemplateName="SPSPERS#0" />
  <!-- SharePoint Server Publishing Infrastructure - Scope: Site -->
  <FeatureSiteTemplateAssociation Id="D0DF37FC-060A-4F57-AE3F-310C2D438551" TemplateName="SPSPERS#0" />
  <!-- Custom Publishing Layouts -->
  <FeatureSiteTemplateAssociation Id="98D8A361-DDF6-4771-A285-64D23E0E556C" TemplateName="SPSPERS#0" />
  <!-- Feature receiver for setting custom master (code)-->
</Elements>

Notice that some other features should be activated before you can set your own master. (Se the complete list of features here – for use in other scenarios)
The “Custom Publishing Layouts” feature is my own custom feature that holds my custom masterpages, pagelayouts, styles etc.
The “Feature receiver for setting custom master” is the feature that reference my feature receiver (se step 2)
Notice that the sitedefinition for Personal sites are named SPSPERS#0 (See a complete list of sitetemplates here)

The scope of this feature should be farm (Farm) – Se this post if you have trouble with the error: “The Project Item “[Item Name]” cannot be deployed through a Feature with Farm  – It sure as hell gave me some headaches – dammit!
scope” when trying to deploy your solution/WSP

4. Deploy the solution and try to create a new personal site.

If I have left something out please let me know

Advertisements

  1. #1 by Jeff on March 19, 2012 - 9:45 pm

    Very helpful, yet for a newbie, where do I place the stapling?

  2. #2 by Jeff on March 20, 2012 - 11:37 pm

    Is it possible to look at what the solution looks like please? I am trying to make this work, but am not having any luck.

  3. #3 by anilkuchi on May 3, 2012 - 7:54 pm

    Hi How can we apply the same branding to my site blog i tried with all combinations but i am not able to activate it .Can you help me

  4. #4 by Adam on June 1, 2012 - 7:18 am

    So no hope for applying this approach in a Sandbox/BPOS/Office 365. Is there a workable alternative?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: