神刀安全网

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,
    Create Master Pages, Custom Page Layouts and Extending Themes
  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.
    Create Master Pages, Custom Page Layouts and Extending Themes
  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.
    public override void FeatureActivated(SPFeatureReceiverProperties properties)    {        SPSite site = properties.Feature.Parent as SPSite;        SPWeb web = site.OpenWeb();        string newCustomMasterUrl = "/_catalogs/masterpage/MyCustomMasterPage.master";        string newMasterUrl = "/_catalogs/masterpage/MyCustomMasterPage.master";        string relativeUrl = (site.ServerRelativeUrl);        if (relativeUrl == @"/")        relativeUrl = "";        newCustomMasterUrl = relativeUrl + newCustomMasterUrl;        newMasterUrl = relativeUrl + newMasterUrl;        if (!string.Equals(newCustomMasterUrl, web.CustomMasterUrl,StringComparison.OrdinalIgnoreCase)       && !string.Equals(newMasterUrl, web.MasterUrl, StringComparison.OrdinalIgnoreCase))        {            web.MasterUrl = newMasterUrl;            web.CustomMasterUrl = newCustomMasterUrl;            web.Update();        }    }    

    Also write the below code in FeatureDeactivating Event receiver.

  23. public override void FeatureDeactivating (SPFeatureReceiverProperties properties)    {        SPSite site = properties.Feature.Parent as SPSite;        SPWeb web = site.OpenWeb();        string newCustomMasterUrl = "/_catalogs/masterpage/seattle.master";        string newMasterUrl = "/_catalogs/masterpage/seattle.master";        string relativeUrl = (site.ServerRelativeUrl);        if (relativeUrl == @"/")        relativeUrl = "";        newCustomMasterUrl = relativeUrl + newCustomMasterUrl;        newMasterUrl = relativeUrl + newMasterUrl;        if (!string.Equals(newCustomMasterUrl, web.CustomMasterUrl, StringComparison.OrdinalIgnoreCase)        && !string.Equals(newMasterUrl, web.MasterUrl, StringComparison.OrdinalIgnoreCase))        {            web.MasterUrl = newMasterUrl;            web.CustomMasterUrl = newCustomMasterUrl;            web.Update();        }    }   
  24. Open the MyCustomMasterPage.master and add text “Hello world” as shown in below image.
  25. Save your changes and Build the project.
  26. The final folder/file structure should look like this:
  27. Right Click on the project node and select Deploy
  28. 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 :

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Create Master Pages, Custom Page Layouts and Extending Themes

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址