By: Hesham Saad | Updated: 2010-07-22 | Comments | Related: > Sharepoint Design
Problem
Many SharePoint portals implement and apply the MySite feature (the MySite feature is only available in MOSS 2007 and is not provided by WSS 3.0). But how about branding the same look and feel of the parent site to MySite? Unfortunately Microsoft designed MySite to have a default format and does not allow custom branding to be effective for all private MySite users. In this tip we'll go through how to overcome this issue.
Solution
Before going deeply into the solution, I want to first clarify the MySite feature structure.
SharePoint MySite has two site collections:
- Public / Shared Site Collection : (My Profile = MySite)
- This public/shared site collection applied to the entire portal can have custom branding (i.e.: Custom master page) using Microsoft SharePoint designer 2007.
- Private Site Collection : (My Home = Personal) {Here We Go !}
- This private site collection can not be branded via Microsoft SharePoint designer and be effective for all private users, because for each user it creates a separate sub-site (webs) under the private site collection which has a different master page. So the only effective solution and best practices is to create a "Feature Stapling" which is the process in which a feature can be attached with site templates, so that a site can be created with certain pre-activated features.
Here are the detailed steps to create our feature stapling.
Enable "Publishing feature" for both My Home (Private) & My Profile (Shared - Public MySite web application) in order to activate and enable the "Master Page" change settings under the "Look and Feel" section for both :
- Click on the Site Actions menu > Site Settings > Site Collection features (Under : "Site Collection Administration" section)
- Click on the Activate button for "Office SharePoint Server Publishing Infrastructure".
For quickly browsing to the targeted features pages type the below URLs in your browser:
- For My Profile (Public Shared / MySite): http://<server_header>/MySite/_layouts/ManageFeatures.aspx?Scope=Site
- For My Home (Private / Personal): http://<server_header>/personal/<user_name>/_layouts/ManageFeatures.aspx?Scope=Site
Creating the stapler feature. We can do it quickly and manually via the below procedures:
- Go to the below targeted features 12 hive folder: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES
- Create a new directory (ex: CustomMySiteFeature).
- Put your custom master page (ex: CustomMySite.master) in this folder which will be applied to your MySite Look and Feel and create two empty XML files (feature.xml and elements.xml).
- Open and edit "feature.xml" via a suitable editor and paste the below code:
<?xml version="1.0" encoding="utf-8"?> <Feature Id="6cca294a-4c6f-4715-a98f-876dceead182" Title="CustomMySiteFeature" Description="Description for CustomMySiteFeature" Version="12.0.0.0" Hidden="FALSE" Scope="Site" DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/" ReceiverAssembly="CustomMySiteFeatureStapling, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2c09f0dec64aee86" ReceiverClass="CustomMySiteFeatureStapling.MySiteFeatureStapler"> <ElementManifests> <ElementFile Location="CustomMySite.master"/> <ElementManifest Location="elements.xml"/> </ElementManifests> </Feature>
The ReceiverAssembly tag defines a declaration for the custom code stapler which will switch / change the initial default master page without custom branded ones (CustomMySite.master).
Open and edit the new "elements.xml" and paste the below code:
<?xml version="1.0" encoding="utf-8" ?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="CustomMySiteMasterPage" RootWebOnly="true" Url="_catalogs/masterpage"> <File Url="CustomMySite.master" Type="GhostableInLibrary" /> </Module> </Elements>
Creating the custom code stapler as referenced by "ReceiverAsembly" in the feature.xml file:
- Open Visual Studio.NET (2005,2008).
- File > New > Project > Class Library (C#) (ex: Name : MySiteFeatureStapler) .
- Reference the "Microsoft. SharePoint" DLL from the ISAPI directory "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI".
- Inherit from "SPFeatureReceiver" interface.
- Sign the DLL using a strongly name signing (Right Click on your Solution > Properties > Sign).
- Paste the below code snippet in your "MySiteFeatureStapler.cs" class code file
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using System.Diagnostics; namespace CustomMySiteFeatureStapling { public class MySiteFeatureStapler : SPFeatureReceiver { public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSecurity.RunWithElevatedPrivileges(delegate() { EventLog eventlog = new EventLog(); eventlog.Source = "MySiteFeatureStapler"; eventlog.WriteEntry("Starting Activation of the MySite Master Page Switcher Feature"); try { SPSite mysite = properties.Feature.Parent as SPSite; SPWeb myweb = mysite.RootWeb; if (myweb.WebTemplate == "SPSPERS") // SPSPERS is the private My Home Web Template { using (myweb) { myweb.Description = "MySite :: " + DateTime.Now.ToShortDateString(); if (myweb.MasterUrl.Contains("default.master")) { myweb.MasterUrl = myweb.MasterUrl.Replace("default.master", "CustomMySite.master"); } myweb.Update(); eventlog.WriteEntry("Found site using 'SPSPERS' template & updated it with the custom master page"); } } if (myweb.WebTemplate == "SPSMSITEHOST") // SPSMSITEHOST is the public shared My Profile Web template { using (myweb) { myweb.Description = "MySite :: " + DateTime.Now.ToShortDateString(); if (myweb.MasterUrl.Contains("default.master")) { myweb.MasterUrl = myweb.MasterUrl.Replace("default.master", "CustomMySite.master"); } myweb.Update(); eventlog.WriteEntry("Found site using 'SPSMSITEHOST' template & updated it with the custom master page"); } } } catch (Exception e) { eventlog.WriteEntry(String.Format("Error activating MySite master page switcher feature {0} : ", e.Message)); } }); } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPSecurity.RunWithElevatedPrivileges(delegate() { EventLog eventlog = new EventLog(); eventlog.Source = "MySiteFeatureStaple"; eventlog.WriteEntry("Starting De-activation of the MySite Master Page Switcher Feature"); try { SPSite mysite = properties.Feature.Parent as SPSite; SPWeb myweb = mysite.RootWeb; if (myweb.WebTemplate == "SPSPERS") { using (myweb) { if (myweb.MasterUrl.Contains("CustomMySite.master")) { myweb.MasterUrl = myweb.MasterUrl.Replace("CustomMySite.master", "default.master"); myweb.ApplyTheme("none"); // i.e.: Initial Default Theme. } myweb.Update(); eventlog.WriteEntry("Removed custom master page from site using 'SPSPERS' template"); } } if (myweb.WebTemplate == "SPSMSITEHOST") { using (myweb) { if (myweb.MasterUrl.Contains("CustomMySite.master")) { myweb.MasterUrl = myweb.MasterUrl.Replace("CustomMySite.master", "default.master"); myweb.ApplyTheme("none"); // i.e.: Initial Default Theme. } myweb.Update(); eventlog.WriteEntry("Removed custom master page from site using 'SPSMSITEHOST' template"); } } } catch (Exception e) { eventlog.WriteEntry(String.Format("Error de-activating MySite master page switcher feature {0} : ", e.Message)); } }); } public override void FeatureInstalled(SPFeatureReceiverProperties properties) { } public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { } } }
- Build and then drag and drop the generated strongly signed DLL to the GAC (Global Assembly Cash) [Run> assembly].
- Make a recycling pool operation [Run> iisreset].
Install and activate our stapler feature which we created in step 2:
- Open command prompt (Run > cmd)
- Change the directory to this path: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
- Type this command for installing our feature: stsadm -o installfeature -name <feature_name>
- Then you can open the Central Administration > Operations > Solution Management in order to deploy our feature to a selected web application(s) then open the targeted site collections (Public and Private) and activate the feature at Site Collection features or you can use this command: stsadm -o activatefeature -name <feature_name>-url <http://mysite_web_app>
For My Profile we'll do it only once, but for the My Home we have do it for every user which is a headache and inconvenient. So below I show a PowerShell script to dynamically and automatically enable the feature for both My Profile and My Home without having to enable it manually. Also we can create another holder feature which will execute the stapler feature.
Install-SPFeature -path "<New_Nav_Feature>" Enable-SPFeature -identity "<New_Nav_Feature>" -URL http://<mysitehost> (Enables the new feature on the mysitehost) //Enable the new feature on all personal sites: $personalSites = get-spsite | where {$_.RootWeb.WebTemplate -eq "SPSPERS"} foreach ($site in $personalSites) {Enable-SPFeature -Identity "<New_Nav_Feature>" -Url $site.Url}
Next Steps
- Check out MSSQLTips.com for great information about Microsoft SQL Server.
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips
Article Last Updated: 2010-07-22