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?