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:
- Navigate to the site in your preferred web browser. It should look like the following screenshot:
- Select Site settings from the Settings menu.
- Select Master page from the Look and Feel section, as shown in the following screenshot:
- Select the site master page and the system master page to use. In this example, we will use the oslo master page:
- Click on OK to save the changes. Now, the site will look like the following screenshot:
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.
Follow these steps to change the site master pages using PowerShell:
- Use the
Get-SPWeb
Cmdlet to get the SharePoint site:$web = Get-SPWeb http://sharepoint/site
- Set the
MasterUrl
andCustomMasterUrl
properties to configure the master pages by their URLs:$web.MasterUrl = "/_catalogs/masterpages/seattle.master" $web.CustomMasterUrl = "/_catalogs/masterpages/seattle.master"
- Use the
Update
method to apply the changes:$web.Update()
- Use the
Dispose
method to discard theSPWeb
object:$web.Dispose()
Follow these steps to change the site master pages with code using the server-side object model:
- Open the site collection containing the site in a
using
statement:using (var site = new SPSite("http://sharepoint/site"))
- Open the site in a
using
statement:using (var web = site.OpenWeb())
- Set the
MasterUrl
andCustomMasterUrl
properties to configure the master pages by their URLs:web.MasterUrl = "/_catalogs/masterpages/seattle.master"; web.CustomMasterUrl = "/_catalogs/masterpages/seattle.master";
- Use the
Update
method to apply the changes:web.Update();
See also
- The How to: Apply a master page to a site in SharePoint 2013 article on MSDN at http://msdn.microsoft.com/en-us/library/jj862339.aspx
- The SPWeb class topic on MSDN at http://msdn.microsoft.com/en-us/library/Microsoft.SharePoint.SPWeb.aspx
- The SPSite class topic on MSDN at http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.aspx
- The Get-SPWeb topic on TechNet at http://technet.microsoft.com/en-us/library/ff607807.aspx