Book Image

SharePoint 2013 WCM Advanced Cookbook

By : JOHN CHAPMAN
Book Image

SharePoint 2013 WCM Advanced Cookbook

By: JOHN CHAPMAN

Overview of this book

Table of Contents (19 chapters)
SharePoint 2013 WCM Advanced Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.

Tip

From PowerShell or .NET code, the individual properties of the list item representing the composed look could be used when applying the individual components.

How to do it...

Follow these steps to apply the composed look:

  1. Navigate to the site in your preferred web browser.

  2. 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.

  3. From the available composed looks, click on the preview image to select a composed look.

  4. Before trying out the selected composed look, we can change the background image, color palette, site layout (master page), and font scheme.

    Note

    Changing the various options will update the live preview automatically.

  5. Select Try it out to preview the composed look and your configured options live on your SharePoint site.

  6. 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.

Applying a composed look using PowerShell

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.

Note

PowerShell scripts are plain text files with a .ps1 file extension. You can create and edit them with Notepad, however applications such as PowerShell ISE provide additional editing capabilities that assist in writing PowerShell scripts.

Follow these steps to apply a composed look with PowerShell:

  1. Use the Get-SPWeb Cmdlet to get the SharePoint site:

    $web = Get-SPWeb http://sharepoint/site
    
  2. Use the 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))
  3. Use the Update method to apply the changes:

    $web.Update()
  4. Use the Dispose method to discard the SPWeb 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.

Applying a composed look with code using the server-side object model

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:

  1. Open the site collection containing the site in a 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.

  2. 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 the SPWeb object represents a site.

  3. Use the 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);
  4. Use the Update method to apply the changes:

    web.Update();

See also