7

We are almost complete with a large project.

I am trying to find out which rendering in the page has been configured to use data source using a Powershell script. So that we can run in both UAT and Prod to make sure all rendering data source has been configured properly.

Has anyone done this before?

  • Can you elaborate your question with what you've tried or thought about so far? – Martin Davies Oct 6 '16 at 4:59
10

Based on your question I think you need something that will require a bit more finesse, so here's my attempt - It will only show renderings that have no datasource, but need them

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")
}

function Test-ContainRenderingsWithoutDataSources  {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
        [Sitecore.Data.Items.Item]$Item)

    #Test Versioned Layout
    $BrokenRendering = Get-Rendering -Item $Item -FinalLayout | Where-Object { ($_.Datasource -eq "") -and (Test-DataSourceRequired $_.ItemID) }
    $BrokenRendering | %{ Write-Host "$($Item.Paths.Path) - '$((Get-Item master: -ID $_.ItemId).Name)' has no datasource" -ForegroundColor Red } 

}

Get-Item -Path master:\content\Home | Test-ContainRenderingsWithoutDataSources

In this case I'm checking whether the rendering that we are inspecting (that has no datasource specified) actually requires one. By requires I understand that the rendering has a value in "DataSource Location" and "DataSource Template". that filters out false positives where renderings that don't need datasource would be shown.

You can pipe multiple items into the cmdlet as well like:

Get-ChildItem -Path master:\content\Home -Recurse | Test-ContainRenderingsWithoutDataSources

Sample output:

enter image description here

5

The simplest solution would be to inspect the renderings of every item and print out the ID of an item and the value of the Datasource:

Get-ChildItem /sitecore/content/home -Recurse | Get-Rendering |? { $_.Datasource -ne "" } | ft OwnerItemId, Datasource

You can further work on the Format-Table (ft) syntax to get more appealing result.

5

You can use the DataSource parameter together with a Wildcard filter to only select renderings that have a Datasource:

Get-ChildItem -Recurse | Get-Rendering -Datasource "*"

Remember that, in Sitecore 8 onwards, this will target the Shared Layout. If you want to get the Final Layout you should also specify the -FinalLayout parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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