Hi,
Here is a quick guide for branding SharePoint 2013 My Sites using Feature Stapling.
Select "Farm solution" then provide the correct URL to your MySite Host (in my case I'm using "/my") then click Finish.
Step 1 : Provisioning the custom masterpage
Edit the Elements.xml and copy this:
Step 2 : Applying the custom masterpage to the site
We'll use an event receiver to apply the new master.
Add the following code:
Step 3 : Stapling the "apply mastepage" 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 "/my/personal/xxx" :)
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 (or think PowerShell if needed)
Add the followin code
Your projet should look like this:
Step 4 : Applying the masterpage to any subsite created under the my site
Then enter the following code:
Your project should look like this:
Deploy your solution, activate the Farm feature and connect with a new user to his MySite check that the new master is applied. (alternative is to delete your current MySite and connect to the site to force automatic re-creation)
Your done :)
Edit: These are clues on how to solve common problems
* "The newsfeed and about me pages are styled, but looking at the urls these are all default screens and don't belong the personal mysites of individuals. Pages like blogs and apps have /personal/*username*/ in the url and these are not being properly styled as part 4 should be doing."
=> This means that the stapler feature is not working (step 3). Check the "Farm_MySiteStapler" feature, it should be in charge of activating the "Site_ApplyCustomMasterPage" feature for each new personal site created. If the personal site already exists you should run a PowerShell activation for each of them as they didn't initialised with the stapled feature.
Try Mat's code it should do the trick (Thx Mat):
* "My custom masterpage gets applied to the About me and the Newsfeed but not the blog, tasks and apps pages."
=>This means the event receiver "ER_MySite_ElementsWebProvisioned" is not working. Check that the ER is in your feature. You can use SharePoint Manager (http://spm.codeplex.com/) to see the EventReceivers collection of the SPSite object or through PowerShell
Our custom ER should be listed.
Here is a quick guide for branding SharePoint 2013 My Sites using Feature Stapling.
Select "Farm solution" then provide the correct URL to your MySite Host (in my case I'm using "/my") then click Finish.
Step 1 : Provisioning the custom masterpage
As a starting point just copy the original my sites masterpage from "\15\TEMPLATE\FEATURES\MySiteMaster\mysite15.master" into the project and rename it "CustomMySite.master" and integrate your HTML/CSS design to it. EDIT : if you are adding a new file (css, png, jpg,...) that you are referencing in the master consider using the LAYOUTS folder to keep url reference simple. Alternatively you can use a provisioning feature to store it in a library instance on the site but this method can be unsafe as the url may be inconsistent and therefore will require to unghost the masterpage to edit the html.
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>
Step 2 : Applying the custom masterpage to the site
We'll use an event receiver to apply the new master.
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 mastepage" 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 "/my/personal/xxx" :)
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 (or think PowerShell if needed)
Add the followin 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".
Your projet should look like this:
At this point the branding will actually work for the root sites, if you wish to apply template to any subsite that will be created (such as Blogs,...) you need to follow step 4 to hook the WebProvisioned event.
Step 4 : Applying the masterpage to any subsite created under the my site
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(); } } }
Your project should look like this:
Deploy your solution, activate the Farm feature and connect with a new user to his MySite check that the new master is applied. (alternative is to delete your current MySite and connect to the site to force automatic re-creation)
Your done :)
Edit: These are clues on how to solve common problems
* "The newsfeed and about me pages are styled, but looking at the urls these are all default screens and don't belong the personal mysites of individuals. Pages like blogs and apps have /personal/*username*/ in the url and these are not being properly styled as part 4 should be doing."
=> This means that the stapler feature is not working (step 3). Check the "Farm_MySiteStapler" feature, it should be in charge of activating the "Site_ApplyCustomMasterPage" feature for each new personal site created. If the personal site already exists you should run a PowerShell activation for each of them as they didn't initialised with the stapled feature.
Try Mat's code it should do the trick (Thx Mat):
Get-SPSite http://{your-server-name}/my/personal/* | ForEach { $_.URL } | ForEach { Enable-SPFeature -Identity {your-solution-name}_Site_ApplyCustomMasterPage -URL $_ }
* "My custom masterpage gets applied to the About me and the Newsfeed but not the blog, tasks and apps pages."
=>This means the event receiver "ER_MySite_ElementsWebProvisioned" is not working. Check that the ER is in your feature. You can use SharePoint Manager (http://spm.codeplex.com/) to see the EventReceivers collection of the SPSite object or through PowerShell
$site = Get-SPSite http://mydevsite/my/personal/djavanroa $site.EventReceivers
Our custom ER should be listed.
Comments
I am getting error when activating the feature in site collection when include event receiver on step 4. But feature is working when I end up with step 3 and deploy and activate feature. Any comments on this.
Thanks
Raj
Custom master page is not copied to the sub sites (Newly created) and feature is not activated. When I activate the feature manually, file not found error page appears. I think this is because of no custom master page found in the master page library.
Any suggestions
Thanks
Raj
Best regards
This all works great! When a Blog site is created can someone please tell me how I can activate another custom feature.
New to this so apologies if a daft question
from MSDN:
"Best practice is to dispose explicitly of individual Web sites that are retrieved from the collection that is returned through the AllWebs property."
see:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.allwebs.aspx
and
http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx
;)
http://wellytonian.com/2012/07/sharepoint-2013-site-templates-powershell-codes/
The Project Item "ML_ApplyCustomMasterPageStapler" cannot be deployed through a Feature with Farm scope.
I am new to sharepoint so I may be missing something basic. Please let me know where could I possible have gone wrong.
Thanks,
Shamak
Great guide and works 100% for me - with no visual basic experience.
However I would like to change something I assume quite simple but having no luck.
This feature replaces both custom and default master pages. What would I need to change in order for it to just replace the "default" master and not both which includes the "Custom Master Page"?
Reason being is the my custom master page is not compatible with system pages such as dialog form, when you share or when one creates a new page etc.
Thanks in advance!
right click ->add new item->empty element
MySites, Newsfeed, About Me, all have the new style. Blog & Sites, on the other hand, are just a blue band across the top of the screen with my name on the right with an About Me and Sign Out option in it's menu. I've deleted my Personal\SXF site and tried again, with no luck. Any ideas?
Tried to use Sharepoint Manager 2013 but don't know what and where to look.
Was anyone able to get past this? Would really appreciate some insight if you have!
Thanks,
Shereen
I can work around this by using powershell on the server to go through all the personal sites and activate the feature, but I have to run this every time a new user accesses the site or they will be left with the wrong styling. (As it does no harm I may just run this on a schedule every day anyway).
For anyone who wants it (or can improve on it) the powershell is here...
Get-SPSite http://{your-server-name}/my/personal/* | ForEach { $_.URL } | ForEach { Enable-SPFeature -Identity {your-solution-name}_Site_ApplyCustomMasterPage -URL $_ }
Cannot initialize the following SharePoint project item: 'ER_MySite_ElementsWebProvisioned'. This item requires a type provider that has the following ID, but this provider could not be found: 'Microsoft.VisualStudio.SharePoint.EventHandler'. Reinstall the extension that provides this item type, or remove the item from your project.
Project: ClientName.MySitesBranding
Can someone please help??
Thanks , Nice Post . its working for existing user but in my scnerio , I want stapling for new mysite(SharePoint 2013) user .
For example : I am new in my site personal ,then first time I am using or filling mysite activity but it didn't changes the master page as expected stapler behaviour .
If anyone have any idea so please eloborate me.
If there is any change in master page, do we need to redeploy everything?
I had an issue On MySite Feature Stapling, my code is working fine but the Site collection level Feature is Not activating default at the time of Provisioning the Site Collection. If i Activate that Site collection level Feature manually it's working Fine
Any Idea on this issue much Appreciated if you come back with solution
Thank you.
I'm just curious, why does the stapler only activate the feature on new MySites and not ALL new sites?
With this in mind I also added a check to the feature activating that checks to see if the root web of the site is a mysite template, otherwise it won't activate the feature...we have different branding for other webs so I want to make sure they can't be activated there.
if (site.RootWeb.WebTemplate=="SPSPERS" && site.RootWeb.Configuration==2){
(activating code here...)
} else {
throw new SPException("This site isn't a MySite");
}
Thanks,
Raj
Thank you for this post. I am facing the same issue which many people reported here. My custom branding is coming on default page of my site, NewsFeed. But not coming on Blog site. I also executed the powershell scripts which you mentioned. Itt's returing message that this Feayure is already activated at scope of users mysite. Any suggestions?
Thank you for this post. I have one query, which some users have already asked. In my scenario, the custom master page branding is not getting applied to Blog site. I have executed the Powershell command also. It says that the Feature is already activated. Any suggestion?
Thanks,
Sanjay
If anyone having "This site has not been shared with you" issue, you can try checkin the major version of the master page. This solved issue in my case.
Sharepoint Training in Chennai