Background
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....
Introduction
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
="1.0"="utf-8"
<Feature Id="F9AB6989-A3A8-4df2-8254-76987DD4FAA5" Title="SiteStaplerFeature"
Scope="Farm" Version="1.0.0.0"
Hidden="FALSE"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/"> <ElementManifests>
<ElementManifest Location="Elements.xml" />
</ElementManifests>
</Feature>
element.xml for stapler feature
="1.0"="utf-8"
<Elements Id="F52F9522-4086-4341-8B88-4368139C1E37"
xmlns="http://schemas.microsoft.com/sharepoint/" >
<FeatureSiteTemplateAssociation
Id="f6924d36-2fa8-4f0b-b16d-06b7250180fa"
TemplateName="SPSPERS#0"/>
<FeatureSiteTemplateAssociation
Id="6ED16B2A-209F-4531-BBCE-F7B3F0CB75A8"
TemplateName="SPSPERS#0"/>
<FeatureSiteTemplateAssociation
Id="06927A49-4124-4a91-BA61-DB0BD5155BF2"
TemplateName="SPSPERS#0"/>
</Elements>
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
="1.0"="utf-8"
<Feature Id="6ED16B2A-209F-4531-BBCE-F7B3F0CB75A8" Title="MySiteMasterPage"
Scope="Site"
Version="1.0.0.0"
Hidden="FALSE"
DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/"
ReceiverAssembly="MyAssembly.MyNameSpace, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=282025704650b387"
ReceiverClass="MyAssembly.MyNameSpace.FeatureRecieverClass">
<ElementManifests>
<ElementManifest Location="MySiteMasterPage\Module.xml" />
<ElementFile Location="MySiteMasterPage\MySite.master" />
</ElementManifests>
</Feature>
Module.xml for masterpage feature
="1.0"="utf-8"
<Elements Id="D8669972-4B6C-49b2-9A34-430DD5C296F6"
xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="MySiteMasterPage" List="116" Url="_catalogs/masterpage">
<File Path="MySiteMasterPage\MySite.master" Url="MySite.master"
Type="GhostableInLibrary" />
</Module>
</Elements>
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
<Feature xmlns=http://schemas.microsoft.com/sharepoint/
Id="06927A49-4124-4a91-BA61-DB0BD5155BF2"
Scope="Web"
Hidden="False"
Title="MySitePages"
Description="Deploys a custom page with some web parts.">
<ElementManifests>
<ElementManifest Location="MySitePages/elements.xml" />
</ElementManifests>
<ActivationDependencies>
<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" />
</ActivationDependencies>
</Feature>
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
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="Pages" List="850" Url="Pages">
<File Name="SearchResult.aspx" Url="SearchResult.aspx"
Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" >
<AllUsersWebPart WebPartZoneID="Header" WebPartOrder="1">
<![CDATA[
</AllUsersWebPart>
</File>
</Module></Elements>
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 AllUsersWebPart
tag.
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.
Conclusion
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.
History
- 8th September, 2009: Initial post