3

I'm creating a script that would list down all the components in the a particular page then displays the
RenderingItemName, RenderingItemID, RenderingItemPath, RenderingDatasource, etc.,

I found the script below somewhere in the internet and slightly modify it. The script works for getting the information I need except for the datasource which is always blank.

Please help.

write-host 'Running script...'
Set-Location master:\sitecore\content\NL\site1 
$pages = get-item master:\sitecore\content\NL\site1\Splash | get-childitem -Recurse
$device = Get-LayoutDevice -Default
$Results = @();
$DataPath = "C:\Components\ComponentsInUse.csv"     

function Test-DataSourceRequired {
    [CmdletBinding()]
    param([Sitecore.Data.ID]$RenderingId)

    $rendering = Get-Item master: -ID $RenderingId
    
    # if data source location and data source template are empty then it's ok for the rendering to have no data source defined
    return -not [string]::IsNullOrEmpty($rendering."Datasource Location") -or -not [string]::IsNullOrEmpty($rendering."Datasource Template")
}
 
foreach($page in $pages){
 
    $renderings = Get-Rendering -Item $page -Device $device -FinalLayout -Language "nl-nl" | Where-Object { (Test-DataSourceRequired $_.ItemID) }
 
    foreach($rendering in $renderings){
 
        if($rendering.ItemID -ne $null)
        {
            $renderingItem = Get-Item master: -ID $rendering.ItemID
            if($renderingItem -ne $null)
            {
                $Properties = @{
                    RenderingItemName = $renderingItem.Name
                    RenderingItemID = $renderingItem.ID
                    RenderingItemPath = $renderingItem.Paths.Path
                    RenderingDatasource = $renderingItem.Datasource
                    UsedOnPage = $page.Name
                    UsedOnPageID = $page.ID
                    UsedOnPagePath = $page.Paths.Path
                }
 
                $Results += New-Object psobject -Property $Properties
            }
        }
 
    }
}
 
$Results | Select-Object RenderingItemName,RenderingItemID,RenderingItemPath,RenderingDatasource,UsedOnPage,UsedOnPageID,UsedOnPagePath | Export-Csv -notypeinformation -Path $DataPath
 
write-host 'Script ended'

1 Answer 1

5

You are trying to get Datasource from an item which is retrieved using Get-Item. Instead you should get datasource from Item retrived using Get-Rendering

Change this code

$Properties = @{
                    RenderingItemName = $renderingItem.Name
                    RenderingItemID = $renderingItem.ID
                    RenderingItemPath = $renderingItem.Paths.Path
                    RenderingDatasource = $renderingItem.Datasource
                    UsedOnPage = $page.Name
                    UsedOnPageID = $page.ID
                    UsedOnPagePath = $page.Paths.Path
                }

with

$Properties = @{
                    RenderingItemName = $renderingItem.Name
                    RenderingItemID = $renderingItem.ID
                    RenderingItemPath = $renderingItem.Paths.Path
                    RenderingDatasource = $rendering.Datasource
                    UsedOnPage = $page.Name
                    UsedOnPageID = $page.ID
                    UsedOnPagePath = $page.Paths.Path
                }

Note that $renderingItem.Datasource has been changed with $rendering.Datasource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.