Find rendering references

ok
02 Mar, 24
Sitecore

# The ID of the rendering you're looking for

$renderingId = "{RENDERING-ID}"


# Function to search renderings in fields

function Find-RenderingInField {

    param(

        [Sitecore.Data.Items.Item]$item,

        [string]$fieldId,

        [string]$renderingId

    )


    $field = [Sitecore.Data.Fields.LayoutField]$item.Fields[$fieldId]

    if ($field -and $field.Value.Contains($renderingId)) {

        $layoutXml = [Sitecore.Layouts.LayoutDefinition]::Parse($field.Value)

        foreach ($device in $layoutXml.Devices) {

            foreach ($rendering in $device.Renderings.Renderings) {

                if ($rendering.ItemID -eq $renderingId) {

                    Write-Host "Found rendering in item: $($item.Paths.FullPath)"

                    break

                }

            }

        }

    }

}


# Search in the content items

"master:/sitecore/content" | Get-ChildItem -Recurse | ForEach-Object {

    Find-RenderingInField -item $_ -fieldId "{F1A1FE9E-A60C-4DDB-A3A0-BB5B29FE732E}" -renderingId $renderingId # Shared Layout

    Find-RenderingInField -item $_ -fieldId "{04BF00DB-F5FB-41F7-8AB7-22408372A981}" -renderingId $renderingId # Final Layout

}


# Search in standard values of templates

"master:/sitecore/templates" | Get-ChildItem -Recurse | Where-Object { $_.TemplateName -eq "Template" } | ForEach-Object {

    $standardValues = $_.__StandardValues

    if ($standardValues) {

        $standardValuesItem = Get-Item "master:" -ID $standardValues

        Find-RenderingInField -item $standardValuesItem -fieldId "{F1A1FE9E-A60C-4DDB-A3A0-BB5B29FE732E}" -renderingId $renderingId # Shared Layout

        Find-RenderingInField -item $standardValuesItem -fieldId "{04BF00DB-F5FB-41F7-8AB7-22408372A981}" -renderingId $renderingId # Final Layout

    }

}