Item Renderings
In this section we'll show how to manage item renderings.

List renderings

Example: The following demonstrates the use of Get-LayoutDevice and Get-Rendering to find all renderings on a page associated with the FinalLayout.
1
$defaultLayout = Get-LayoutDevice -Default
2
$rootItem = Get-Item -Path "master:" -ID "{961563FC-3445-4558-BF3A-06DF06BA6298}"
3
Get-Rendering -Item $rootItem -Device $defaultLayout -FinalLayout
Copied!

Update rendering parameters

Example: The following demonstrates the use of Get-Rendering and Set-Rendering for updating values on templates.
1
$rendering = Get-Item -Path "master:\sitecore\layout\Sublayouts\Sample Sublayout"
2
3
$items = Get-ChildItem -Path "master:\sitecore\templates\Sample Item" -Recurse
4
foreach($item in $items) {
5
$renderingInstance = Get-Rendering -Item $_ -Rendering $rendering
6
if ($renderingInstance) {
7
Set-Rendering -Item $_ -Instance $renderingInstance -Parameter @{
8
"Lorem" = "Ipsum"
9
}
10
Write-Host "Updated $($_.Paths.FullPath)"
11
}
12
}
Copied!

Find pages using rendering

Example: The following demonstrates how to report on pages referencing the specified rendering.
1
Get-Item "master:\layout\Renderings\Feature\Experience Accelerator\Page Content\Page Content" |
2
Get-ItemReferrer | Where-Object { $_.ContentPath.StartsWith("/Demo/usa/Home") } | Show-ListView
Copied!

Find renderings marked cacheable

Example: The following demonstrates how to report on which renderings are globally set to "Cacheable".
1
Get-ChildItem -Path "master:\layout\Renderings" -Recurse |
2
Where-Object { $_.Cacheable -eq "1" } |
3
Select-Object -Property Name, Cacheable, ClearOnIndexUpdate, VaryBy* |
4
Sort-Object -Property Name | Show-ListView
Copied!

Find renderings with personalization

Example: The following demonstrates how to find renderings with a conditions node set on the item.
1
$query = "fast:/sitecore/content//*[@__renderings='%<conditions%' or @#__Final Renderings#='%<conditions%']"
2
$items = Get-Item -Path "master:" -Query $query
Copied!

Disable cacheable setting on renderings

Example: The following demonstrates how to disable global caching on all renderings.
1
$VerbosePreference = "Continue"
2
Get-ChildItem -Path "master:\layout\Renderings" -Recurse |
3
Where-Object { $_.Cacheable -eq "1" } |
4
ForEach-Object { Write-Verbose "Disabled global caching on $($_.Name)"; $_.Cacheable = "0" }
5
6
# VERBOSE: Disabled global caching on Navigation
Copied!

Move renderings between placeholders

Example: The following moves renderings from one placeholder to another. See this article for more details.
1
$placeholderMappings = @(
2
@("/old-placeholder","/new-placeholder"),
3
@("/another-old-placeholder","/new-placeholder")
4
)
5
$rootItem = Get-Item -Path master:/sitecore/content/Home
6
$defaultLayout = Get-LayoutDevice "Default"
7
# Toggle for whether to update Shared or Final Layout
8
$useFinalLayout = $True
9
# If set to true, the script will only list the renderings that need fixing, rather than fixing them.
10
$reportOnly = $False
11
foreach ( $item in Get-ChildItem -Item $rootItem -Recurse )
12
{
13
# Only interested in items that have a layout
14
if (Get-Layout $item)
15
{
16
foreach( $mapping in $placeholderMappings )
17
{
18
# Get renderings in this item that have renderings in the placeholder we want to update
19
$renderings = Get-Rendering -Item $item -Placeholder ($mapping[0] + '/*') -Device $defaultLayout -FinalLayout:$useFinalLayout
20
21
foreach ( $rendering in $renderings )
22
{
23
# Only update the rendering if we're not in "Report Only" mode
24
if (!$reportOnly)
25
{
26
# Update the placeholder in the rendering and set it back in the item
27
$rendering.Placeholder = $rendering.Placeholder -replace $mapping[0], $mapping[1]
28
Set-Rendering -Item $item -Instance $rendering -FinalLayout:$useFinalLayout
29
}
30
Write-Host "$($item.FullPath) - Rendering $($rendering.UniqueID) - Placeholder: $($mapping[0]) --> $($mapping[1])"
31
}
32
}
33
}
34
}
Copied!

Remove datasource from rendering

Example: The following removes a datasource from a rendering on the FinalLayout.
1
Get-Rendering -Item $item -PlaceHolder "main" |
2
Foreach-Object { Set-Rendering -Item $item -Instance $_ -DataSource $null -FinalLayout }
Copied!

Replace compatible rendering

1
$rendering = Get-Item master:\layout\path\to\your\rendering
2
$renderingPageContainer = Get-Rendering -Item $item "{F39BAC93-1EEC-446B-A4A1-AB7F7C1B6267}" -Device $defaultLayout
3
$renderingPageContainer.ItemID = $rendering.ID
4
Set-Rendering -Item $item -Instance $renderingPageContainer
Copied!

Further Reading

Last modified 2yr ago