Monday 17 September 2012

Custom master page in SharePoint 2010 using VS 2010

Steps to create a custom master page in sharepoint 2010 using VS2010
  1. Create an empty SharePoint Solution
  2. Add a Module in the Project ( module name branding )
  3. Add a master page(master page name mymaster)
  4. Add the content in the master page
  5. Change the Element file as following 
<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="brading">
  <File Path="brading\mymaster.master" Url="brading/mymaster.master" /></Module></Elements>

Url  ---->  it create the folder structure  exampl branding /mymaster creates the mymaster file inside the branding folder
  1. Add the event receiver as follows to change the master page at the time of feature activated 
    and change the default master page at at the time of feature deactivated
    
    
    // Uncomment the method below to handle the event raised after a feature has been activated.public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb currentWeb = (SPWeb)properties.Feature.Parent;
    
        currentWeb.MasterUrl = "/_catalogs/masterpage/mymaster.master";
        currentWeb.CustomMasterUrl = "/_catalogs/masterpage/mymaster.master";
        currentWeb.Update();
    }
    
    // Uncomment the method below to handle the event raised before a feature is deactivated.public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        SPWeb currentWeb = (SPWeb)properties.Feature.Parent;
    
        currentWeb.MasterUrl = "/_catalogs/masterpage/nightandday.master";
        currentWeb.CustomMasterUrl = "/_catalogs/masterpage/nightandday.master";
        currentWeb.Update();
    }
     
     
    Please note this is for the Site master no need to made much changes in the system master page.
    Just change the logo of the master page that is enough.