Hi Everyone ,
In this article i am explaining how could we attach a custom master page to Personal sites in MySite Host. I have created two Sharepoint Project named MyMasters and MyMastersStapling using Visual Studio 2010.
you can download the visual studio solution from CodePlex
http://mymasters.codeplex.com/
The solution is anwering fallowing questions :
* How to deploy custom master page ?
* How to customize a masterpage ?
* How to attach custom master page to personal sites using staping feature ?
* How to set wellcome page programmatically ?
* How to add document library as a web part to a page ?
MyMasters Project: is a sharepoint project that deploy a custom master page to a sharepoint site.
MyMasterStapling Project: is a sharepoint project that attach feature of MyMasters to personal site template and activates the publishing features

First i created a site scope feature named “MyMasters” and and a feaurereciever .The important part is here the feature guid . You need this guid for feature stapling configuration. you can get the feature guid from Feature manifest file.
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/” Title=”MyMasters” Description=”This feature enables defined master page for your site”
Id=”09c222f7-68ed-4278-a3ce-d64b8dbfb168” ReceiverAssembly=”MyMasters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e49c3c1155d0e27″ ReceiverClass=”MyMasters.Features.MyMasters.MyMastersEventReceiver” Scope=”Site”>
… child nodes
</Feature>
And I have create two modules MasterPageModule and CustomAssests Module .
MasterPageModule : contains masterpage file and when the feature is activated it deploy master page the masterpage library under _catalogs folder.
CustomAssests : contains necessary css , js and image files. when the feature is activated it deploy assests to Style Library List of target site by creating specific folder for each asset type.

Here is the code of Feature Reciever.
public class MyMastersEventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
ApplyTheme(properties);
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
RevertTheme(properties);
SPSite site = (SPSite)properties.Feature.Parent;
if (site != null)
{
//Remove custom master page
SPFile masterFile = site.RootWeb.GetFile(“_catalogs/masterpage/PersonalSite.master”);
masterFile.Delete();
//Remove assets folders.
site.RootWeb.Folders[“Style Library”].SubFolders.Delete(“CustomCssFiles”);
site.RootWeb.Folders[“Style Library”].SubFolders.Delete(“CustomJSFiles”);
site.RootWeb.Folders[“Style Library”].SubFolders.Delete(“CustomImages”);
}
} private void ApplyTheme(SPFeatureReceiverProperties properties)
{
SPSite site = (SPSite)properties.Feature.Parent;
if (site != null)
{
// Set the System Master Page to orginal
Uri masterUri = new Uri(site.RootWeb.Url + “/_catalogs/masterpage/v4.master”);
site.RootWeb.MasterUrl = masterUri.AbsolutePath;
// Set the Publishing Master Page our custom PersonalSite.master page.
Uri customMasterUri = new Uri(site.RootWeb.Url + “/_catalogs/masterpage/PersonalSite.master”);
site.RootWeb.CustomMasterUrl = customMasterUri.AbsolutePath;
site.RootWeb.Update();
}
}
private void RevertTheme(SPFeatureReceiverProperties properties)
{
SPSite site = (SPSite)properties.Feature.Parent;
if (site != null)
{
// Set the System Master Page to orginal
Uri masterUri = new Uri(site.RootWeb.Url + “/_catalogs/masterpage/v4.master”);
site.RootWeb.MasterUrl = masterUri.AbsolutePath;
// Set the Publishing Master Page to orginal
Uri customMasterUri = new Uri(site.RootWeb.Url + “/_catalogs/masterpage/v4.master”);
site.RootWeb.CustomMasterUrl = customMasterUri.AbsolutePath;
site.RootWeb.Update();
}
}
}
By default the following rule applies when you deploy a master page:
- Site Master Pages: used by all publishing pages – and only by publishing pages
- System Master Pages: used by everything else including forms and view pages
So in Feature reciever -> ApplyTheme() function we set two master page first one is V4.master the orginal master for System masterpage and our custom master for Site Master page. As you know you have to enable Publishing Features for the site if you want to this deplotment work correctly.
For deploying PersonalSite.master via module the element file :
<?xmlversion=“
1.0“
encoding=“
utf-8“
?>
<Elementsxmlns=“
http://schemas.microsoft.com/sharepoint/“
>
<ModuleName=“
MasterPageModule“
List=“
116“
Url=“
_catalogs/masterpage“
>
<FilePath=“
MasterPageModule\PersonalSite.master“
Url=“
PersonalSite.master“
Type=“
GhostableInLibrary“
>
<PropertyName=“
UIVersion“
Value=“
4“
/>
<PropertyName=“
ContentTypeId“
Value=“
0x010105“
/>
</File>
</Module>
</Elements>
You can deploy this solution any site by using visual studio at the end you can able to see this view :

For MyMasterStapling Project . I have created a farm level feature named “MyMastersStapling” and an empty element named “StaplingElement”

Element.xml :
<?xmlversion=“
1.0“
encoding=“
utf-8“
?>
<Elementsxmlns=“
http://schemas.microsoft.com/sharepoint/“
>
<FeatureSiteTemplateAssociationId=“
f6924d36-2fa8-4f0b-b16d-06b7250180fa“
TemplateName=“
SPSPERS#0“
/>
<FeatureSiteTemplateAssociationId=“
22a9ef51-737b-4ff2-9346-694633fe4416“
TemplateName=“
SPSPERS#0“
/>
<FeatureSiteTemplateAssociationId=“
09c222f7-68ed-4278-a3ce-d64b8dbfb168“
TemplateName=“
SPSPERS#0“
/>
</Elements>
The FeatureSiteTemplateAssociation element maps feature GUIDs to site defintions – note that the format of the TemplateName attribute value is <SiteDefName>#<ConfigurationID>. This obviously allows a degree of flexibility and allows you to do fairly complex things with different configurations of site definitions. As you now at the beginning of article we highlighted a GUID the feature of MyMasters.
The third item is using this guid 09c222f7-68ed-4278-a3ce-d64b8dbfb168 .What about first two ? these feature ids are blong to Publishing Features. First one is “Publishing Feature Site” feature’s id at site level and the second one is Publishing feature’s id at web level.
These two ids are built in sharepoint 2010 and it is not change by installation .If you wonder how could i found this ids ,i used the powershell console for sharepoint :
get-spfeature | where-object { $_.DisplayName -like “*Publish*” }

For Template name SPSPERS is the personal site template name. for #Zero i am attaching the default configuration.
For More information about site templates.
http://office.microsoft.com/en-us/sharepoint-server-help/a-preview-of-the-sharepoint-server-2010-site-templates-HA101907564.aspx
So far so good. After you deploy our stapling project and activate the feature , the users can able to see our custom master page even if self site creation is enabled for personal sites .
See you next articles.