SharePoint 2013 WCM Advanced Cookbook
上QQ阅读APP看书,第一时间看更新

Changing the site master pages

Master pages are a feature of the ASP.NET web application framework that SharePoint leverages to provide a consistent look and feel for all pages within a SharePoint site. These can be used to provide various styling and branding configurations. SharePoint Server 2013 ships with two master pages that can be applied to SharePoint 2013 sites: seattle and oslo. The seattle master page is the default used when creating new SharePoint sites.

Each SharePoint site uses two configured master pages: the site master page and the system master page. The site master page is used when displaying content pages, libraries, lists, and so on, whereas the system master page is used when displaying settings and administrative pages.

SharePoint 2013 allows site collections to be configured to run in SharePoint 2010 or SharePoint 2013 compatibility modes. Master pages are only made available to the compatibility mode they are designed for. Thus, SharePoint 2010 master pages cannot be applied to a SharePoint 2013 site and vice versa.

Getting ready

In order to change the master page settings for a SharePoint site, the SharePoint Server Publishing Infrastructure site collection feature and SharePoint Server Publishing site feature must be activated.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you

How to do it...

Follow these steps to change the site master pages:

  1. Navigate to the site in your preferred web browser. It should look like the following screenshot:
    How to do it...
  2. Select Site settings from the Settings menu.
  3. Select Master page from the Look and Feel section, as shown in the following screenshot:
    How to do it...
  4. Select the site master page and the system master page to use. In this example, we will use the oslo master page:
    How to do it...
  5. Click on OK to save the changes. Now, the site will look like the following screenshot:
    How to do it...

How it works...

The site relative URL for the selected site master page is assigned to the MasterUrl property of the SPWeb object representing the current site and the site relative URL for the system master page is set to the CustomMasterUrl property. The SPWeb object is then updated and saved to the SharePoint database.

There's more...

Site master pages may also be applied with PowerShell or code using the server-side object model.

Changing the site master pages using PowerShell

Follow these steps to change the site master pages using PowerShell:

  1. Use the Get-SPWeb Cmdlet to get the SharePoint site:
    $web = Get-SPWeb http://sharepoint/site
    
  2. Set the MasterUrl and CustomMasterUrl properties to configure the master pages by their URLs:
    $web.MasterUrl = "/_catalogs/masterpages/seattle.master"
    $web.CustomMasterUrl = "/_catalogs/masterpages/seattle.master"
    
  3. Use the Update method to apply the changes:
    $web.Update()
    
  4. Use the Dispose method to discard the SPWeb object:
    $web.Dispose()
    

Changing the site master pages with code using the server-side object model

Follow these steps to change the site master pages with code using the server-side object model:

  1. Open the site collection containing the site in a using statement:
    using (var site = new SPSite("http://sharepoint/site"))
  2. Open the site in a using statement:
    using (var web = site.OpenWeb())
  3. Set the MasterUrl and CustomMasterUrl properties to configure the master pages by their URLs:
    web.MasterUrl = "/_catalogs/masterpages/seattle.master";
    web.CustomMasterUrl = "/_catalogs/masterpages/seattle.master";
  4. Use the Update method to apply the changes:
    web.Update();

See also