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 master page and logo settings to all sites in a farm


For this recipe, we are using a PowerShell script to apply master page and logo settings to each SharePoint site in every site collection of each web application on the local SharePoint farm.

How to do it...

Follow these steps to apply master page and logo settings to all sites in the local SharePoint farm using PowerShell:

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

  2. Use a foreach loop to iterate through each content of SPWebApplication on the local SharePoint farm using the Get-SPWebApplication Cmdlet:

    foreach($webApp in (Get-SPWebApplication))
    
  3. Use a foreach loop to iterate through each SPSite in the Sites property of the SPWebApplication object:

    foreach($site in $webApp.Sites)
    
  4. Verify the CompatibilityLevel property of SPSite to ensure it is in SharePoint 2013 (Version 15) mode and not in SharePoint 2010 (Version 14) mode.

    if ($site.CompatibilityLevel –eq 15)
    
  5. Use a foreach loop to iterate through each SPWeb in the AllWebs property of the SPSite object:

    foreach ($web in $site.AllWebs)
    
  6. Check if the SPWeb object exists:

    if ($web.Exists)
    
  7. Set the master page and logo properties for the SPWeb object:

    $web.SiteLogoUrl = "/SiteAssets/logo.png"
    
    $web.SiteLogoDescription = "My PowerShell Site"
    
    $web.MasterUrl = "/_catalogs/masterpages/seattle.master"
    
    $web.CustomMasterUrl = "/_catalogs/masterpages/seattle.master"
    
  8. Use the Update method on the SPWeb object to save the changes:

    $web.Update()
    
  9. Use the Dispose method to discard the SPWeb object:

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

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

  12. Execute the script in the PowerShell session:

    ./applymasterpageandlogo.ps1
    

How it works...

In this recipe, we retrieved all of the content web applications using the Get-SPWebApplication Cmdlet. We then iterated through each site collection in the Sites property of each web application and then iterated through each site in the AllWebs property of each site collection. For each site, we updated the properties for the logo and master pages.

There's more...

The steps performed in PowerShell may also be completed in code using the server-side object model. Follow these steps to apply master page and logo settings to all sites on the local SharePoint farm with code using the server-side object model:

  1. Use a foreach loop to iterate through each content SPWebApplication on the local SharePoint farm:

    foreach (var webApp in SPWebService.ContentService.WebApplications)
  2. Use a foreach loop to iterate through each SPSite in the Sites property of the SPWebApplication object:

    foreach (var site in webApp.Sites)
  3. Verify the CompatibilityLevel property of SPSite to ensure it is in SharePoint 2013 (Version 15) mode and not in SharePoint 2010 (Version 14) mode:

    if (site.CompatibilityLevel == 15)
  4. Use a foreach loop for iterating through each SPWeb in the AllWebs property of the SPSite object:

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

    if (web.Exists)
  6. Set the master page and logo properties on the SPWeb object:

    web.SiteLogoUrl = "/SiteAssets/logo.png";
    
    web.SiteLogoDescription = "My Code Site";
    
    web.MasterUrl = "/_catalogs/masterpages/seattle.master";
    web.CustomMasterUrl = 
      "/_catalogs/masterpages/seattle.master";
  7. Use the Update method on the SPWeb object to save the changes:

    web.Update();
  8. Use the Dispose method to discard the SPSite and SPWeb objects:

    web.Dispose();
    site.Dispose();