0

I'm new to Sitecore PowerShell and would like to know 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 of using PowerShell to create a more elegant solution.

I reviewed the documentation on Get-Rendering and Set-Rendering but couldn't 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, 2021 at 2:38
  • yes, that's the rendering parameter I'm trying to set it to.
    – Arik Torhn
    Nov 8, 2021 at 14:02

2 Answers 2

4

Try this code, I have modified the custom rendering parameter like the Ajax Options check box.

In this, the function takes as an input item you want to edit and field - either Final or Shared layout.

Also Write a log on every step:-

$languages = @('en')

$startPath = '/sitecore/content/xxx/xxx/Presentation/Partial Designs/Frenchs/Recipe'

$renderingNames = @('Menu Wrapper')

$renderingParamterToUpdate = "Ajax Call"

$newRenderingParameterValue = "0"




foreach($language in $languages){

    $items = Get-ChildItem -Path $startPath -Language $language -Recurse

    foreach($item in $items){

        Write-Host 'Updating Item: ' $item.Paths.FullPath

        foreach($itemRendering in Get-Rendering -Item $item -FinalLayout) {

            $renderingItem = Get-Item -Path master: -ID $itemRendering.ItemID    

            if($renderingNames -contains $renderingItem.Name){

                Write-Host 'Found Rendering: ' $($renderingItem.Name)                

                $parameters = Get-RenderingParameter -Rendering $itemRendering

                if($parameters[$renderingParamterToUpdate] -ne $newRenderingParameterValue){

                    Write-Host 'Updating' $renderingParamterToUpdate -ForegroundColor Green

                    $parameters[$renderingParamterToUpdate] = $newRenderingParameterValue

                    $itemRendering | Set-RenderingParameter -Parameter $parameters |

                    Set-Rendering -Item $item -FinalLayout

                }

            }

        }

    }

}
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 and acknowledge you have read our privacy policy.

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