Wednesday, July 4, 2012

Attached SharePoint 2010 Master Page using feature receiver




Hello,


In this post i will explain , how to deploy the master page to the SharePoint Master page gallery and melodramatically using feature receiver apply the master page to the Site Collection.


First create the empty SharePoint project using  the visual studio 2010 named it as SharePointCustom


Now add Sharepoint Module element by right click on the Project - > click Add - > New Item -> Select SharePoint 2010 Section - > Select Module element - >  Give Proper Name - >Click on Add button


I have specified the name like "SharepointCustom.MasterPages"


Now add the existing master page ( v4.Master ) to the Module element from the Sharepoint 14 hives Layouts folder


"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\"


Rename the master page as "SharepointCustom.master"


Add the same master page to set it as a System master and renamed it as "SharepointCustomsSystem.master"


Now open the element.xml file of the master page it will be looked as shown below


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="SharepointCustom.MasterPages"  Url="_catalogs/masterpage">
<File Path="SharepointCustom.MasterPages\SharepointCustom.master" Url="SharepointCustom.MasterPages/SharepointCustom.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="True" />
<File Path="SharepointCustom.MasterPages\SharepointCustomSystem.master" Url="SharepointCustom.MasterPages/SharepointCustomSystem.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="True" />
</Module>
</Elements>


and .spdata file for the master page (by default this file is hidden you can view this file by click on the Show All File option from the solution explorer)


<?xml version="1.0" encoding="utf-8"?>
<ProjectItem Type="Microsoft.VisualStudio.SharePoint.Module" DefaultFile="Elements.xml" SupportedTrustLevels="All" SupportedDeploymentScopes="Web, Site" xmlns="http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel">
  <Files>
    <ProjectItemFile Source="Elements.xml" Target="SharepointCustom.MasterPages\" Type="ElementManifest" />
    <ProjectItemFile Source="SharepointCustom.master" Target="SharepointCustom.MasterPages\" Type="ElementFile" />
    <ProjectItemFile Source="SharepointCustomSystem.master" Target="SharepointCustom.MasterPages\" Type="ElementFile" />
  </Files>
</ProjectItem>


Above changes will add the Master Pages to the Master Page Gallery.


Now add Site level feature to attached master page with the Site.


Named it as "SharepointCustom.MasterPages" and add event receiver by right click on the feature - > click on the "Add Event Receiver".


Now we set Activate On Default property is true by double click on the feature , click any where on the feature surface and click on F4 key.






So final structure of the solution is looked like as below













Now open the code behind file for the feature and uncomment the "FeatureActivated" method.


Add the following lines of code to the method


public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite currentSite = (SPSite)properties.Feature.Parent;
if (currentSite != null)
{
SPWeb currentWeb = currentSite.RootWeb;
Uri systemMasterUri = new Uri(currentWeb.Url + "/_catalogs/masterpage/SharepointCustom.MasterPages/SharepointCustomSystem.master");
Uri customMasterUri = new Uri(currentWeb.Url + "/_catalogs/masterpage/SharepointCustom.MasterPages/SharepointCustom.master");
currentWeb.MasterUrl = customMasterUri.AbsolutePath;
currentWeb.CustomMasterUrl = systemMasterUri.AbsolutePath;
currentWeb.Update();
}
}


So when feature get activated at site level our custom master pages will be attached with the site.


Now add following lines of code to reset the origional master pages when feature will get deactivated


public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite currentSite = (SPSite)properties.Feature.Parent;
if (currentSite != null)
{
SPWeb currentWeb = currentSite.RootWeb;
Uri masterUri = new Uri(currentWeb.Url + "/_catalogs/masterpage/v4.master");
currentWeb.MasterUrl = masterUri.AbsolutePath;
currentWeb.CustomMasterUrl = masterUri.AbsolutePath;
currentWeb.Update();
}
}


Now deploy the solutions.Our custom feature will activated because we have set "Activate On Default" property is true.


Regards
Hiren Patel





1 comment:

  1. Hi Hiren,

    What if I have to attach new master page to every newly created MySite in SP-2010 ? I am sorry if I asked you a dumb question but I am new to SP-2010.

    Will this same feature take care of that ?

    ReplyDelete