0

I'm new to Sitecore powershell and I would like to know what's the best way to get all the items on my content tree that have a specific rendering parameter set so I can replace it with another value in the rendering parameter? I know I can replace the layout IDs but I thought in using powershell to create a more elegant solution.

I have reviewed the documentation on Get-Rendering and Set-Rendering but I wasn't able to get the script to work. Here is what I have so far. Any help is highly appreciated

 $defaultLayout = Get-LayoutDevice "Default"
$path = "master:\sitecore\content\home\page-that-has-rendering"
$language = 'en-US'
$items = Get-ChildItem -Path $path -Language $language -Recurse
$newValue = Get-Item -Path 'master:\sitecore\content\home\path-to-mynewrenderingparametervalue'
$items | ForEach-Object {
    
    $rendering = Get-Rendering -Id $_.ID -Device $defaultLayout -FinalLayout 
    
    #$param = Get-RenderingParameter -Rendering $rendering | Format-Table -Auto
    foreach($itemRendering in Get-Rendering -Item $_ -FinalLayout) {

        $parameters = Get-RenderingParameter -Rendering $itemRendering
        Set-Rendering -Instance $newValue     
        
    }
}
2
  • What's the path that you have for $newValue? Is that another item that you're copying the rendering parameters from? Nov 6 at 2:38
  • yes, that's the rendering parameter I'm trying to set it to.
    – Arik Torhn
    Nov 8 at 14:02
0

Try with this code snippet. I am not using build-in cmdlets but Sitecore API only.

Function takes as an input item that you want to edit and field - either Final or Shared layout.

Rendering parameter name and value are hardcoded inside function. Modify it to your specific needs.

function Set-RenderingParametersForField($item, $fieldID) {
    $laytoutField = New-Object "Sitecore.Data.Fields.LayoutField" $item.Fields[$fieldID]
    $modified = $false
    $definition = [Sitecore.Layouts.LayoutDefinition]::Parse($laytoutField.Value)
    $definition.Devices | % {
        $_.Renderings | % {
            $par = [Sitecore.Web.WebUtil]::ParseUrlParameters($_.Parameters)
            $renderingParameter = $par["RENDERING_PARAMETER_FIELD_NAME"]
            if ($renderingParameter -ne $null) {
                $par["RENDERING_PARAMETER_FIELD_NAME"] = "NEW VALUE"
                $_.Parameters = (New-Object "Sitecore.Text.UrlString" $par).GetUrl()
                $modified = $true
            }
        }
    }
    if ($modified) {
        $item.Editing.BeginEdit()
        $laytoutField.Value = $definition.ToXml()
        $item.Editing.EndEdit() > $null
    }
}

Set-RenderingParametersForField $currentItem ([Sitecore.FieldIDs]::LayoutField)
Set-RenderingParametersForField $currentItem ([Sitecore.FieldIDs]::FinalLayoutField)
2

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.