0

I am looking for powershell script to get datasource item of a rendering. Right now I am getting path of datasource item. But since I am using SXA, I expect few paths like, local:/Data/Hero Banner Also I could not find any relevant property of RenderingItem object that could suffice my need. Any help is appreciated. Thanks in advance.

$itemRenderings = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout

    foreach ( $renderingItem in $itemRenderings ){
        $renderingItem.Datasource
}
1
  • Not really. It gives path and as I mentioned, its not always a path in case of SXA. So I am looking for generic logic to get datasource item. Jun 30, 2023 at 20:56

1 Answer 1

3

You can try something like this for handling paths like, local:/Data/Hero Banner

$itemRenderings = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout

foreach ($renderingItem in $itemRenderings) {
    $dataSourcePath = $renderingItem.Datasource

    # Check if the data source path has a special prefix
    if ($dataSourcePath.StartsWith("local:")) {
        # Handle "local:" prefix
        $dataSourceItemPath = $dataSourcePath.Replace("local:", "$item.Paths.Path")
        $dataSourceItem = Get-Item -Path $dataSourceItemPath
    }
    else {
        # Default handling without any prefixes
        $dataSourceItemPath = $dataSourcePath
        $dataSourceItem = Get-Item -Path $dataSourceItemPath
    }

    # Output the data source item
    $dataSourceItem
}

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