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.
Follow these steps to apply master page and logo settings to all sites in the local SharePoint farm using PowerShell:
.ps1
script file.foreach
loop to iterate through each content of SPWebApplication
on the local SharePoint farm using the Get-SPWebApplication
Cmdlet:foreach($webApp in (Get-SPWebApplication))
foreach
loop to iterate through each SPSite
in the Sites
property of the SPWebApplication
object:foreach($site in $webApp.Sites)
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)
foreach
loop to iterate through each SPWeb
in the AllWebs
property of the SPSite
object:foreach ($web in $site.AllWebs)
SPWeb
object exists:if ($web.Exists)
SPWeb
object:$web.SiteLogoUrl = "/SiteAssets/logo.png" $web.SiteLogoDescription = "My PowerShell Site" $web.MasterUrl = "/_catalogs/masterpages/seattle.master" $web.CustomMasterUrl = "/_catalogs/masterpages/seattle.master"
Update
method on the SPWeb
object to save the changes:$web.Update()
Dispose
method to discard the SPWeb
object:$web.Dispose()
Dispose
method to discard the SPSite
object:$site.Dispose()
PS1
file, for example, applymasterpageandlogo.ps1
../applymasterpageandlogo.ps1
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.
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:
foreach
loop to iterate through each content SPWebApplication
on the local SharePoint farm:foreach (var webApp in SPWebService.ContentService.WebApplications)
foreach
loop to iterate through each SPSite
in the Sites
property of the SPWebApplication
object:foreach (var site in webApp.Sites)
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)
foreach
loop for iterating through each SPWeb
in the AllWebs
property of the SPSite
object:foreach (var web in site.AllWebs)
SPWeb
exists:if (web.Exists)
SPWeb
object:web.SiteLogoUrl = "/SiteAssets/logo.png"; web.SiteLogoDescription = "My Code Site"; web.MasterUrl = "/_catalogs/masterpages/seattle.master"; web.CustomMasterUrl = "/_catalogs/masterpages/seattle.master";
Update
method on the SPWeb
object to save the changes:web.Update();
Dispose
method to discard the SPSite
and SPWeb
objects:web.Dispose(); site.Dispose();
18.119.28.108