1

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.,

The script works for getting the information I need except for the datasource,

But for Datasource I am getting Datasource ItemID , How Can I get Datasource Path

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 = $rendering.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'

2 Answers 2

1

You can achieve this as below. Get the Item from id and then get its path.

$item = Get-Item master: -ID $rendering.Datasource

$item.Paths.FullPath

So your script will be like this -

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){
        $datasouceItem = Get-Item master: -ID $rendering.Datasource        
        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 = $datasouceItem.Paths.FullPath
                    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'
0

You can get the rendering datasource path by adding the below code:

 $item =Get-Item -Path "master:" -ID $rendering.Datasource
 $renderingDataSourcePath=$item.Paths.Path

and add $renderingDataSourcePath in the below code:-

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

If you are getting the ItemId in datasource then add this code. If you are getting datasource as a path then add it directly in the variable.

if(-not [string]::IsNullOrEmpty($rendering.Datasource))
 {
     if(Test-IsGuid -ObjectGuid $rendering.Datasource)
       {
         $item =Get-Item -Path "master:" -ID $rendering.Datasource
         $renderingDataSourcePath=$item.Paths.Path
       }
}
else
{
$renderingDataSourcePath=$rendering.Datasource
}

We add datasource as Id as well as path so we need to check both cases.

Your script will be like this :-

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(-not [string]::IsNullOrEmpty($rendering.Datasource))
            {
                if(Test-IsGuid -ObjectGuid $rendering.Datasource)
                {
                  $item =Get-Item -Path "master:" -ID $rendering.Datasource
                  $renderingDataSourcePath=$item.Paths.Path
                }
            }
            else
            {
                 $renderingDataSourcePath=$rendering.Datasource
            }

            if($renderingItem -ne $null)
            {
                $Properties = @{
                    RenderingItemName = $renderingItem.Name
                    RenderingItemID = $renderingItem.ID
                    RenderingItemPath = $renderingItem.Paths.Path
                    RenderingDatasource = $renderingDataSourcePath
                    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'

Your Answer

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

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