Here is a quick guide for
branding SharePoint 2013 My Sites using Feature Stapling.
Create a new SharePoint Solution.
Select "Farm solution" then provides
the correct URL to your My Site Host then click Finish.
Step 1: Provisioning the custom master page
Add a new Module to the project.
As a
starting point just copy the original my sites master page
from "\15\TEMPLATE\FEATURES\MySiteMaster\mysite15.master" into
the project and rename it "CustomMySite.master" and integrate your
HTML/CSS design to it.
Edit
the Elements.xml and copy this:
<Elements
xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="ML_MasterPages"
List="116" Url="_catalogs/masterpage"
Path="ML_MasterPages" RootWebOnly="TRUE">
<File
Url="CustomMySite.master" Type="GhostableInLibrary" />
</Module>
</Elements>
You'll have to add the module to the
feature you will create in step 2.
Step 2: Applying the custom master page to the site
We'll use an event receiver to apply the new master.
Add a new Feature with scope "Site" and name it
"Site_ApplyCustomMasterPage";
Add an event receiver to the feature.
Add
the following code:
namespace ClientName.MySitesBranding.Features.Site_ApplyCustomMasterPage
{
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
[Guid("{your Guid}")]
public class
Site_ApplyCustomMasterPageEventReceiver : SPFeatureReceiver
{
public override void
FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = (SPSite)properties.Feature.Parent;
string masterUrl =
SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl,
"_catalogs/masterpage/CustomMySite.master");
foreach (SPWeb web in
siteCollection.AllWebs)
{
try
{
web.MasterUrl = masterUrl;
web.CustomMasterUrl =
masterUrl;
web.Update();
}
catch
{
if (web != null)
web.Dispose();
throw;
}
if (web != null)
web.Dispose();
}
}
public override void
FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite siteCollection =
(SPSite)properties.Feature.Parent;
string masterUrl =
SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl,
"_catalogs/masterpage/mysite15.master");
foreach (SPWeb web in siteCollection.AllWebs)
{
try
{
web.MasterUrl = masterUrl;
web.CustomMasterUrl =
masterUrl;
web.Update();
}
catch
{
if (web != null)
web.Dispose();
throw;
}
if (web != null)
web.Dispose();
}
}
}
}
Step 3: Stapling the "apply mast page" feature to the personal site template
this action will hook your feature to any new personal site that will be created after staple is effective. If you need to apply the branding to existing personal write a PowerShell script that activates the feature for each
NOTE
: As the MySiteHost (the "/my" root
url) will usually already exist on the customer's target platform when they
create their "User Profile Application Service" you'll have to
manually activate the "ApplyCustomMasterPage" feature
on that site collection.
Create
a new Feature with scope "Farm" and name it
"Farm_MySiteStapler".
Then create a new empty Element name it
"ML_ApplyCustomMasterPageStapler".
Add the following code
<Elements
xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureSiteTemplateAssociation
Id="{the Guid of the Site_ApplyCustomMasterPage feature}"
TemplateName="SPSPERS#2" />
</Elements>
Note:
that in SharePoint 2013 the Template to staple is "SPSPERS#2"
whereas in SharePoint 2010 you had to staple "SPSPERS#0".
Add the module to the Stapler feature.
Your project should look like this:
At this point the branding will actually
work for the root sites, if you wish to apply template to any sub site
that will be created (such as Blogs,...) you need to follow step 4 to hook the
WebProvisioned event.
Step 4 : Applying the master page to any sub
site created under the my site
Add a new Event receiver
"ER_MySite_ElementsWebProvisioned".
Selected the web event "a site was
provisioned”.
Then enter the following code:
Then enter the following code:
namespace
ClientName.MySitesBranding.Receivers.ER_MySite_ElementsWebProvisioned
{
using System;
using Microsoft.SharePoint;
public class
ER_MySite_ElementsWebProvisioned : SPWebEventReceiver
{
public override void
WebProvisioned(SPWebEventProperties properties)
{
SPWeb web = properties.Web;
SPWeb rootWeb = web.Site.RootWeb;
web.MasterUrl = rootWeb.MasterUrl;
web.CustomMasterUrl =
rootWeb.CustomMasterUrl;
web.Update();
}
}
}
Now add the Event receiver to the
"Site_ApplyCustomMasterPage" feature.
Your project should look like this:
Deploy your solution, activate the Farm feature
and connect with a new user to his My Site check that the new master is
applied. (Alternative is to delete your current My Site and connect to the site
to force automatic re-creation)
Cheers
Suresh
No comments:
Post a Comment