- SharePoint 2013 WCM Advanced Cookbook
- John Chapman
- 473字
- 2021-07-19 18:36:42
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.
How to do it...
Follow these steps to apply a composed look to all sites in a site collection using PowerShell:
- Open your preferred text editor to create the
.ps1
script file. - Get the site collection with the
Get-SPSite
Cmdlet:$site = Get-SPSite http://sharepoint/site
- Use a
foreach
loop to iterate through eachSPWeb
in theAllWebs
property of theSPSite
object:foreach ($web in $site.AllWebs)
- Check if
SPWeb
exists:if ($web.Exists)
- 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)
- Use the
Dispose
method to discard theSPWeb
object:$web.Dispose()
- Use the
Dispose
method to discard theSPSite
object:$site.Dispose()
- Save the file as a
PS1
file, for example,applycomposedlook.ps1
. - 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:
- Open the site collection in a
using
statement:using (var site = new SPSite("http://sharepoint/site")
- Use a
foreach
loop to iterate through eachSPWeb
in theAllWebs
property of theSPSite
object:foreach (var web in site.AllWebs)
- Check if the
SPWeb
exists:if (web.Exists)
- 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);
- Use the
Dispose
method to discard theSPWeb
object:web.Dispose();
See also
- The Themes overview for SharePoint 2013 article on MSDN at http://msdn.microsoft.com/en-us/library/jj927174.aspx
- The SPWeb class topic on MSDN at http://msdn.microsoft.com/en-us/library/Microsoft.SharePoint.SPWeb.aspx
- The SPSite class topic on MSDN at http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.aspx
- The Get-SPSite topic on TechNet at http://technet.microsoft.com/en-us/library/ff607950.aspx