Applying a composed look
When applying an existing composed look to an existing SharePoint site, it is important to note that the only method available for applying the composed look as it exists in the _catalogs/design
list is with the SharePoint web interface. To apply the components of a composed look with PowerShell or .NET code, each property must be specified individually. In this recipe, we will use the SharePoint web interface to apply a composed look as well as use PowerShell and .NET code to apply the components of a composed look.
How to do it...
Follow these steps to apply the composed look:
- Navigate to the site in your preferred web browser.
- Navigate to the Change the look page. We can do this in two ways:
- Select Change the look from the Settings menu.
- Select Site settings from the Settings menu. Then select Change the look from the Look and Feel section.
- Select Change the look from the Settings menu.
- From the available composed looks, click on the preview image to select a composed look.
- Before trying out the selected composed look, we can change the background image, color palette, site layout (master page), and font scheme.
- Select Try it out to preview the composed look and your configured options live on your SharePoint site.
- If you are satisfied with the design changes, select Yes, keep it to apply the styling. Otherwise, select No, not quite there to return to the previous screen.
How it works...
An SPWeb
object represents a SharePoint site in the SharePoint database and server-side object model. When we apply a composed look, the color palette, font scheme, and background image are used to create a new SPTheme
object and it is assigned to the ThemeInfo
property of the SPWeb
object. The site layout, which is a reference to the URL of a master page, is assigned to the MasterUrl
(used for system and settings pages) and CustomMasterUrl
(used for content pages) properties of the SPWeb
object. The SPWeb
object is then saved to the SharePoint database.
When previewing the design changes live on your SharePoint site, SharePoint appends query strings to the home page of the site to instruct the site to use the provided theme information instead of what is currently configured. This is displayed within IFRAME
on the page to allow us to preview the SharePoint site, but not interact with it.
There's more...
A composed look may also be applied with PowerShell or code using the server-side object model.
To launch PowerShell with the SharePoint snap-in loaded, you can select SharePoint 2013 Management Shell from the Start menu. You can also launch Windows PowerShell from the Start menu and manually load the SharePoint snap-in with the following command:
Add-PSSnapin Microsoft.SharePoint.PowerShell
You will see the SharePoint 2013 Management Shell command prompt as shown in the following screenshot:
In addition, the Windows PowerShell ISE application provides the PowerShell command prompt with a user interface to simply create and execute PowerShell scripts.
Follow these steps to apply a composed look with PowerShell:
- Use the
Get-SPWeb
Cmdlet to get the SharePoint site:$web = Get-SPWeb http://sharepoint/site
- Use the
ApplyTheme
method to apply the color palette, font scheme, and background image by their URLs. Specifyfalse
for the last parameter to instruct SharePoint to place the files generated for this theme within the current site:$web.ApplyTheme("/_catalogs/theme/15/Palette015.spcolor", "/_catalogs/theme/15/fontscheme001.spfont", "/images/background.png", $false))
- Use the
Update
method to apply the changes:$web.Update()
- Use the
Dispose
method to discard theSPWeb
object:$web.Dispose()
Tip
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.
Interacting with the server-side object model in C# requires a reference to the Microsoft.SharePoint.dll
assembly found at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI
. In addition, the code must be running in a .NET
context on the SharePoint server. This includes, but is not limited to, Windows services, Windows applications, PowerShell Cmdlets, SharePoint timer jobs, SharePoint web parts, and SharePoint application pages.
Follow these steps to apply a composed look 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"))
If opening the
SPSite
orSPWeb
objects from code without theusing
statement, dispose of the objects when you are done with them. This ensures that the objects are removed from memory and clears up connection resources for SharePoint. - Open the site in a
using
statement:using (var web = site.OpenWeb())
In the SharePoint databases and server-side object model, the
SPSite
object represents a site collection and theSPWeb
object represents a site. - Use the
ApplyTheme
method to apply the color palette, font scheme, and background image by their URLs. Specifyfalse
for the last parameter to instruct SharePoint to place the files generated for this theme within the current site:web.ApplyTheme("/_catalogs/theme/15/Palette015.spcolor", "/_catalogs/theme/15/fontscheme001.spfont", "/images/background.png", false);
- Use the
Update
method to apply the changes:web.Update();
See also
- The Themes overview for SharePoint 2013 article on MSDN at http://msdn.microsoft.com/en-us/library/jj927174.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