Skip to content

Instantly share code, notes, and snippets.

@Wesley-Lomax
Last active June 14, 2019 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wesley-Lomax/98cf209ef678f91c1fa1a13230fd34d0 to your computer and use it in GitHub Desktop.
Save Wesley-Lomax/98cf209ef678f91c1fa1a13230fd34d0 to your computer and use it in GitHub Desktop.
Find Sitecore items with renderings greater than provided parameter
# HasTooManyRenderings determines if the specified item has more than the specified number of renderings.
$startItem = Get-Item -Path master:\content\
$maxRenderings = 30
$resultsPageSize = 30
$props = @{
Title = "Count renderings on items"
Description = "This report will analyse the branch and will tell you which items have a renderings count greater than the provided parameters ."
Width = 450
Height = 300
OkButtonName = "Proceed"
CancelButtonName = "Abort"
Parameters = @(
@{ Name = "startItem"; Title="Root Item"; Tooltip="Branch you want to analyse."},
@{ Name = "maxRenderings"; Title="Max renderings allowed"; Tooltip="Max renderings allowed."}
)
}
$result = Read-Variable @props
if($result -ne "ok") {
Close-Window
Exit
}
function HasTooManyRenderings {
param(
$Item
)
$sharedLayoutField = $item.Fields[[Sitecore.FieldIDs]::LayoutField]
$finalLayoutField = $item.Fields[[Sitecore.FieldIDs]::FinalLayoutField]
$sharedLayoutXml = [Sitecore.Data.Fields.LayoutField]::GetFieldValue($sharedLayoutField)
$finalLayoutXml = [Sitecore.Data.Fields.LayoutField]::GetFieldValue($finalLayoutField)
$sharedLayout = [Sitecore.Layouts.LayoutDefinition]::Parse($sharedLayoutXml)
$finalLayout = [Sitecore.Layouts.LayoutDefinition]::Parse($finalLayoutXml)
($sharedLayout.Devices[0].Renderings.Count -gt $maxRenderings) -Or ($finalLayout.Devices[0].Renderings.Count -gt $maxRenderings)
}
#CMDLET to check whether an item has layout or not. Used to determine whether it's a component or not
function Has-Layout{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0)]
[Sitecore.Data.Items.Item]$Item
)
$layout = Get-Layout -Item $Item
#Get-Layout returns the layout item. If null means that no presentation is set on the FinalLayout
if($layout){
return $Item;
}
return "";
}
# Get-ItemWithTooManyRenderings gets all the items under the start path.
function Get-ItemWithTooManyRenderings {
$items = Get-ChildItem -Path $startItem.Paths.FullPath -Recurse | Where-Object { (Has-Layout $_) -ne "" }
foreach($item in $items) {
if(HasTooManyRenderings($item)) {
$item
}
}
}
# Setup a hashtable to make a more readable script.
$props = @{
InfoTitle = "Too many renderings"
InfoDescription = "Lists all items that have more than " + $maxRenderings + " renderings."
PageSize = $resultsPageSize
}
# Passing a hashtable to a command is called splatting. Call Show-ListView to produce
# a table with the results.
Get-ItemWithTooManyRenderings |
Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },
@{Label="Updated"; Expression={$_.__Updated} },
@{Label="Updated by"; Expression={$_."__Updated by"} },
@{Label="Created"; Expression={$_.__Created} },
@{Label="Created by"; Expression={$_."__Created by"} },
@{Label="Path"; Expression={$_.ItemPath} }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment