Create Master Pages, Custom Page Layouts and Extending Themes

Master pages provide the consistent look and feel and standard behavior that you want for all of the pages in your site. Together with content pages, they produce output that combines the layout of the master page with content from the content page. You can specify all of the shared elements of your site in the master page or pages, and add content page-specific elements to content pages.

Master page gives us an ability for change them at one place and we don’t have to customize all of the pages that use the master page.

There are many enhancements in SharePoint 2013 that affect the overall and user interface as well as how users experience a SharePoint site.

This document illustrates how to,

  1. Create a Master Page using VS2012 & SPD 2013.
  2. Creating Custom Page Layout.
  3. Understanding and Extending Themes

Exercise 1 – Create a Master Page using VS2012

This task will guide you through creating a Master Page using Visual Studio 2012 Solution.

  1. Create a SharePoint Site collection using the Publishing Portal site template. We will be using this site throughout the exercise for debugging. Refer to lab <<Lab01- Creating Web Application and Site Collection in SharePoint 2013>> for steps to create a site collection.

  2. In Visual Studio 2012 and from the menu select File | New | Project....

  3. Select the Visual C# | Office/SharePoint | SharePoint Solutions | SharePoint 2013 - Empty Project template.

  4. Change the Name to CustomMasterPage.

  5. Click Ok.

  6. Leave other fields with their default values and click OK. The SharePoint Customization Wizard appears.

  7. In the local site do you want to use for debugging? Combo box, type the URL of the publishing site created at step a.

  8. Click Finish,

    new

  9. Right click on Project Node and Click Add New Item and select Module from SharePoint Node.

  10. Change the Name to MyCustomMasterPage and click on Add,

  11. This will create Elements.xml and Sample.txt file.

  12. Rename the Feature that has been created as MyCustomMasterPage.

  13. Double Click on Feature and open the designer and Change the Scope to Site.

    site

  14. Go to C:\Program Files\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\Global.

  15. Copy the seattle.master

  16. Paste it under the MyCustomMasterPage Module (in the Visual Studio project)

  17. Rename the file myCustomMasterPage.master

  18. Delete sample.txt from MyCustomMasterPage Module which is not required.

  19. Change the elements.xml file as below

  20. Url:

    This is how we tell SharePoint to put the file in the master page gallery. _catalogs/masterpage in the location of the master page gallery when dealing with site collections. We know we are dealing with site collections because the feature.xml file has a scope of “Site”.

  21. File:

    This tells SharePoint the name of the file it is going to put at the master page gallery. The type is “GhostableInLibrary” – this means the file is still going to be on the 15 hive and SharePoint is going to keep a Ghosted reference to the file.

  22. Right click on feature node and add an event receiver to it and write below code in FeatureActivated Event.
    1. public override void FeatureActivated(SPFeatureReceiverProperties properties)   
    2. {   
    3.     SPSite site = properties.Feature.Parent as SPSite;   
    4.     SPWeb web = site.OpenWeb();   
    5.     string newCustomMasterUrl = "/_catalogs/masterpage/MyCustomMasterPage.master";   
    6.     string newMasterUrl = "/_catalogs/masterpage/MyCustomMasterPage.master";   
    7.     string relativeUrl = (site.ServerRelativeUrl);   
    8.     if (relativeUrl == @"/")   
    9.     relativeUrl = "";   
    10.     newCustomMasterUrl = relativeUrl + newCustomMasterUrl;   
    11.     newMasterUrl = relativeUrl + newMasterUrl;   
    12.     if (!string.Equals(newCustomMasterUrl, web.CustomMasterUrl,StringComparison.OrdinalIgnoreCase)  
    13.     && !string.Equals(newMasterUrl, web.MasterUrl, StringComparison.OrdinalIgnoreCase))   
    14.     {   
    15.         web.MasterUrl = newMasterUrl;   
    16.         web.CustomMasterUrl = newCustomMasterUrl;   
    17.         web.Update();   
    18.     }   
    19. }   
    Also write the below code in FeatureDeactivating Event receiver.
    1. public override void FeatureDeactivating (SPFeatureReceiverProperties properties)   
    2. {   
    3.     SPSite site = properties.Feature.Parent as SPSite;   
    4.     SPWeb web = site.OpenWeb();   
    5.     string newCustomMasterUrl = "/_catalogs/masterpage/seattle.master";   
    6.     string newMasterUrl = "/_catalogs/masterpage/seattle.master";   
    7.     string relativeUrl = (site.ServerRelativeUrl);   
    8.     if (relativeUrl == @"/")   
    9.     relativeUrl = "";   
    10.     newCustomMasterUrl = relativeUrl + newCustomMasterUrl;   
    11.     newMasterUrl = relativeUrl + newMasterUrl;   
    12.     if (!string.Equals(newCustomMasterUrl, web.CustomMasterUrl, StringComparison.OrdinalIgnoreCase)   
    13.     && !string.Equals(newMasterUrl, web.MasterUrl, StringComparison.OrdinalIgnoreCase))   
    14.     {   
    15.         web.MasterUrl = newMasterUrl;   
    16.         web.CustomMasterUrl = newCustomMasterUrl;   
    17.         web.Update();   
    18.     }   
    19. }  
  23. Open the MyCustomMasterPage.master and add text “Hello world” as shown in below image.

  24. Save your changes and Build the project.

  25. The final folder/file structure should look like this:

  26. Right Click on the project node and select Deploy

  27. Browse to the site where the solution was deployed. You will notice that the new master page has got applied. 
Click on the Site Actions wheel and go to Site Settings -> Web Designer Galleries > Master pages and page layouts and in the list you can see your myCustomMasterPage.
 
Read more articles on SharePoint: