0

I need to update the Datasource Item Path in Item Presentation Details using PowerShell for Sitecore.

Since there are over 100 items, I want to avoid doing this manually. I've written a PowerShell script for this purpose, but it's not working correctly.

Here's my PowerShell script:

# Define the base path where the items are located
$basePath = "/sitecore/content/jss-app/Home/News/socialNews/"

# Define the template GUID for the "socialNews" item
$socialResponsibilityTemplateId = "{42CE662C-173F-4597-A3FB-5DE1C9E724C8}"

# Define the template GUID for the datasource item
$datasourceTemplateId = "{391E2206-916E-415C-A985-E21652337AB8}"

# Get all items under the base path and its descendants that are of the "socialNews" template
$items = Get-ChildItem -Path "master:$basePath" -Recurse | Where-Object {
    $_.TemplateID -eq $socialResponsibilityTemplateId
}

# Iterate through each of the found "socialNews" items
foreach ($item in $items) {
    Write-Host "Processing 'socialNews' item: $($item.Paths.FullPath)"
    
    # Now find the matching datasource item under this item (recursively)
    $datasourceItem = Get-ChildItem -Path $item.Paths.FullPath -Recurse | Where-Object {
        $_.TemplateID -eq $datasourceTemplateId
    }
    
    if ($datasourceItem) {
        Write-Host "Found matching datasource item: $($datasourceItem.Paths.FullPath)"
        
        # Access the Presentation settings of the item
       $defaultLayout = Get-LayoutDevice -Default       
       $presentationList = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout

        # Iterate through each rendering applied to the item
        foreach ($rendering in $presentationList) {
            # Check if the rendering is the "ContentBlock" rendering
            if ($rendering.ItemID -eq "{59A3CE49-41E1-47A8-9EE5-023F1011722D}") {
                Write-Host "Found 'ContentBlock' rendering for item: $($item.Paths.FullPath)"
                
                # Update the datasource for the ContentBlock rendering
                $newDatasourcePath = $datasourceItem.Paths.FullPath
                Write-Host "Updating datasource to: $newDatasourcePath"
                
                # Update the datasource field in the rendering settings
                $rendering.Datasource = $newDatasourcePath
                
                # Save the changes to the presentation
                $item.Editing.BeginEdit()
                $item.Presentation.SetRenderings($presentationList, $false)
                # Save the updated renderings back
                $item.Editing.EndEdit()

                Write-Host "Datasource updated for 'ContentBlock' rendering in the layout."
            }
        }
    }
}

Write-Host "Datasource update complete."

here in this line I'm getting error $item.Presentation.SetRenderings($presentationList, $false).

Is there any other way to set a Presentation?

2 Answers 2

1

To update the rendering details on presentation of an item you can use this code Set-Rendering -Item $item -Instance $rendering -DataSource $newDatasourcePath -FinalLayout. The Set-Rendering command in the script is used to update the datasource of a rendering on a Sitecore item. Here I'm adding updated script

# Define the base path where the items are located
$basePath = "/sitecore/content/jss-app/Home/News/socialNews/"

# Define the template GUID for the "socialNews" item
$socialNewsTemplateId = "{42CE662C-173F-4597-A3FB-5DE1C9E724C8}"

# Define the template GUID for the datasource item
$datasourceTemplateId = "{391E2206-916E-415C-A985-E21652337AB8}"

# Get all items under the base path and its descendants that are of the "socialNews" template
$items = Get-ChildItem -Path "master:$basePath" -Recurse | Where-Object {
    $_.TemplateID -eq $socialNewsTemplateId
}

# Iterate through each of the found "socialNews" items
foreach ($item in $items) {
    Write-Host "Processing 'socialNews' item: $($item.Paths.FullPath)"
    
    # Now find the matching datasource item under this item (recursively)
    $datasourceItem = Get-ChildItem -Path $item.Paths.FullPath -Recurse | Where-Object {
        $_.TemplateID -eq $datasourceTemplateId
    }
    
    if ($datasourceItem) {
        Write-Host "Found matching datasource item: $($datasourceItem.Paths.FullPath)"
        
        # Access the Presentation settings of the item
       $defaultLayout = Get-LayoutDevice -Default       
       $presentationList = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout

        # Iterate through each rendering applied to the item
        foreach ($rendering in $presentationList) {
            # Check if the rendering is the "ContentBlock" rendering
            if ($rendering.ItemID -eq "{59A3CE49-41E1-47A8-9EE5-023F1011722D}") {
                Write-Host "Found 'ContentBlock' rendering for item: $($item.Paths.FullPath)"
                $renderingName = $rendering.name
                 Write-Host "RenderingName :  $renderingName"
                # Update the datasource for the ContentBlock rendering
                $newDatasourcePath = $datasourceItem.Paths.FullPath
                Write-Host "Updating datasource to: $newDatasourcePath"
                Set-Rendering -Item $item -Instance $rendering -DataSource $newDatasourcePath -FinalLayout
                Write-Host "Datasource updated for 'ContentBlock' rendering in the layout."
            }
        }
    }
}

Write-Host "Datasource update complete."

Hope this help!!

-1

You will need to use Set-Rendering function to update the datasource on the rendering of the item as below.

# Define the base path where the items are located
$basePath = "/sitecore/content/jss-app/Home/News/socialNews/"

# Define the template GUID for the "socialNews" item
$socialResponsibilityTemplateId = "{42CE662C-173F-4597-A3FB-5DE1C9E724C8}"

# Define the template GUID for the datasource item
$datasourceTemplateId = "{391E2206-916E-415C-A985-E21652337AB8}"

# Get all items under the base path and its descendants that are of the "socialNews" template
$items = Get-ChildItem -Path "master:$basePath" -Recurse | Where-Object {
    $_.TemplateID -eq $socialResponsibilityTemplateId
}

# Iterate through each of the found "socialNews" items
foreach ($item in $items) {
    Write-Host "Processing 'socialNews' item: $($item.Paths.FullPath)"
    
    # Now find the matching datasource item under this item (recursively)
    $datasourceItem = Get-ChildItem -Path $item.Paths.FullPath -Recurse | Where-Object {
        $_.TemplateID -eq $datasourceTemplateId
    }
    
    if ($datasourceItem) {
        Write-Host "Found matching datasource item: $($datasourceItem.Paths.FullPath)"
        
        # Access the Presentation settings of the item
       $defaultLayout = Get-LayoutDevice -Default       
       $presentationList = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout

        # Iterate through each rendering applied to the item
        foreach ($rendering in $presentationList) {
            # Check if the rendering is the "ContentBlock" rendering
            if ($rendering.ItemID -eq "{59A3CE49-41E1-47A8-9EE5-023F1011722D}") {
                Write-Host "Found 'ContentBlock' rendering for item: $($item.Paths.FullPath)"
                
                # Update the datasource for the ContentBlock rendering
                $newDatasourcePath = $datasourceItem.Paths.FullPath
                Write-Host "Updating datasource to: $newDatasourcePath"
                
                # Update the datasource field in the rendering settings
                $rendering.Datasource = $newDatasourcePath
                
                Set-Rendering -Item $item -Instance $rendering -FinalLayout
                
                Write-Host "Datasource updated for 'ContentBlock' rendering in the layout."
            }
        }
    }
}

Write-Host "Datasource update complete."

You can find more details on the Set-Rendering function here.

Hope this helps!!! Let me know in case you face any issues.

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.