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.
Follow these steps to apply the composed look:
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.
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:
Get-SPWeb
Cmdlet to get the SharePoint site:$web = Get-SPWeb http://sharepoint/site
ApplyTheme
method to apply the color palette, font scheme, and background image by their URLs. Specify false
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))
Update
method to apply the changes:$web.Update()
Dispose
method to discard the SPWeb
object:$web.Dispose()
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 FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPI
. 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:
using
statement:using (var site = new SPSite("http://sharepoint/site"))
If opening the SPSite
or SPWeb
objects from code without the using
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.
using
statement:using (var web = site.OpenWeb())
In the SharePoint databases and server-side object model, the SPSite
object represents a site collection and the SPWeb
object represents a site.
ApplyTheme
method to apply the color palette, font scheme, and background image by their URLs. Specify false
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);
Update
method to apply the changes:web.Update();
3.17.76.72