After a long time, I am back to CodeProject. This time, it is with SharePoint. One of the great strengths of SharePoint is collaboration and a core functionality of this feature is my site. My site is a user's personal site where one can do anything from updating one's profile to searching persons, adding them as colleagues, tracking changes in colleagues profile, writing blogs and a lot of other things. But all this is provided in SharePoint default look and feel and one has to be really SharePoint literate to apply his/her own look and feel. What if someone wants to have all users’ my site with a custom branding............. Go ahead....
Described below is how to ensure that my site created by SharePoint users will have custom master pages applied on site also to ensure that my site will contain some predefined pages with specified web parts. You will be happy to know that all this can be achieved with just three features and a few lines of code.
Implementing Branded My Site
Before going into the actual implementation, just refresh what feature stapling is.
Feature Stapling is a process in which a feature can be attached with site templates so that a site can be created with certain pre-activated features.
So what I did was to create a stapler feature which associated the following features with the my site definition:
- Publishing feature with all its pre requisite features (so that I can deploy my custom pages)
- A feature to apply custom master page with my site
- A feature to deploy pages with web parts
feature.xml for stapler feature
<Feature Id="F9AB6989-A3A8-4df2-8254-76987DD4FAA5" Title="SiteStaplerFeature"
<ElementManifest Location="Elements.xml" />
element.xml for stapler feature
FeatureSiteTemplateAssociation is a tag to associate features with a site definition. Here TemplateName="SPSPERS#0" is used as this is the default my site template. Now the feature ids that are referred in element.xml file are as follows:
- The first one (with id
f6924d36-2fa8-4f0b-b16d-06b7250180fa) is the id of publishing infrastructure feature as it is needed to enable publishing feature on site to deploy custom pages.
- The second (with id
6ED16B2A-209F-4531-BBCE-F7B3F0CB75A8) is a custom feature to deploy master page with site and set the default master page.
- And the third (with id
06927A49-4124-4a91-BA61-DB0BD5155BF2) is a custom feature to deploy custom pages.
My Site Master Page feature: This feature contains a custom master page to deploy on my site and the activation code is associated with a feature which will apply the master page as default master page of site.
feature.xml for mysite masterpage feature
<Feature Id="6ED16B2A-209F-4531-BBCE-F7B3F0CB75A8" Title="MySiteMasterPage"
<ElementManifest Location="MySiteMasterPage\Module.xml" />
<ElementFile Location="MySiteMasterPage\MySite.master" />
Module.xml for masterpage feature
<Module Name="MySiteMasterPage" List="116" Url="_catalogs/masterpage">
<File Path="MySiteMasterPage\MySite.master" Url="MySite.master"
Master Page Feature activation Code
public override void Featureactivating(SPFeatureReceiverProperties properties)
SPSite site = (SPSite)properties.Feature.parent;
SPWeb web = site.OpenWeb("");
web.CustomMasterUrl = "/_catalogs/masterpage/MySite.master";web.Update();
Now the third feature which is used to deploy pages with custom web parts on my site is:
Feature.xml for mysitepages feature
Description="Deploys a custom page with some web parts.">
<ElementManifest Location="MySitePages/elements.xml" />
<ActivationDependency FeatureId="22A9EF51-737B-4ff2-9346-694633FE4416" />
<ActivationDependency FeatureId="A392DA98-270B-4e85-9769-04C0FDE267AA" />
<ActivationDependency FeatureId="AEBC918D-B20F-4a11-A1DB-9ED84D79C87E" />
<ActivationDependency FeatureId="89E0306D-453B-4ec5-8D68-42067CDBF98E" />
<ActivationDependency FeatureId="D3F51BE2-38A8-4e44-BA84-940D35BE1566" />
The features in the tag
<ActivationDependencies> are hidden features of SharePoint which require to be activated to activate publishing feature. So you can use this tag as it is in your feature.
Element.xml for mysite pages feature
<Module Name="Pages" List="850" Url="Pages">
<File Name="SearchResult.aspx" Url="SearchResult.aspx"
Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" >
<AllUsersWebPart WebPartZoneID="Header" WebPartOrder="1">
A page with name SearchResult.aspx should be present in this feature folder with a webpart zone id "
header". I have used webpart XML for people core search result webpart here. If you want to use custom webpart on a page, just export the webpart and paste webpart XML inside
Points of Interest
The thing that I really liked while implementing this requirement was that I didn't have to write thousands of lines of code. It was all about activating features and just 10 lines of code to apply the master page on site.
Here I have described all we need to create a branded my site for all users. We discussed feature stapling, publishing feature, applying master page on the site and deploying pages with web parts. I tried to keep things as simple as possible. If anyone has any questions, I will be more than happy to reply. I will also try to upload features and code files as soon as possible.
- 8th September, 2009: Initial post