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

Using PowerShell to apply a composed look to all sites in a site collection


Windows PowerShell provides administrators with the ability to create complex scripts that utilize Cmdlets and .NET code. The Microsoft SharePoint PowerShell snap-in exposes many of the common administrative functions of SharePoint as Cmdlets. For the rest, we can use the server-side object model.

Since composed looks are applied at the site level, it can be cumbersome to apply them to a large number of sites. In this recipe, we are going to use PowerShell to iterate through all of the SharePoint sites in a site collection to apply a composed look.

Tip

When using complex PowerShell, it is ideal to write the commands in a text file with a .ps1 extension and then execute the script from the PowerShell session. This allows us to easily use the foreach loops and other techniques that are common to programming.

How to do it...

Follow these steps to apply a composed look to all sites in a site collection using PowerShell:

  1. Open your preferred text editor to create the .ps1 script file.

  2. Get the site collection with the Get-SPSite Cmdlet:

    $site = Get-SPSite http://sharepoint/site
    
  3. Use a foreach loop to iterate through each SPWeb in the AllWebs property of the SPSite object:

    foreach ($web in $site.AllWebs)
    
  4. Check if SPWeb exists:

    if ($web.Exists)
    
  5. Apply the composed look using the ApplyTheme method:

    $web.ApplyTheme("/_catalogs/theme/15/Palette015.spcolor", "/_catalogs/theme/15/SharePointPersonality.spfont", "/_layouts/15/images/image_bg011.jpg", $false)
    
  6. Use the Dispose method to discard the SPWeb object:

    $web.Dispose()
    
  7. Use the Dispose method to discard the SPSite object:

    $site.Dispose()
    
  8. Save the file as a PS1 file, for example, applycomposedlook.ps1.

  9. Execute the script in the PowerShell session:

    ./applycomposedlook.ps1
    

How it works...

Using PowerShell we can easily create scripts to perform tasks that would normally require a tedious amount of manual work. In this recipe, we iterated through each site in the AllWebs property of the site collection that we obtained using the Get-SPSite Cmdlet. For each SharePoint site, we used the ApplyTheme method to apply our composed look.

There's more...

The steps performed in PowerShell may also be completed with code using the server-side object model. Follow these steps to apply a composed look to all sites in a site collection with code using the server-side object model:

  1. Open the site collection in a using statement:

    using (var site = new SPSite("http://sharepoint/site")
  2. Use a foreach loop to iterate through each SPWeb in the AllWebs property of the SPSite object:

    foreach (var web in site.AllWebs)
  3. Check if the SPWeb exists:

    if (web.Exists)
  4. Apply the composed look using the ApplyTheme method:

    web.ApplyTheme("/_catalogs/theme/15/Palette015.spcolor", "/_catalogs/theme/15/SharePointPersonality.spfont", "/_layouts/15/images/image_bg011.jpg", false);
  5. Use the Dispose method to discard the SPWeb object:

    web.Dispose();

See also