Tuesday, March 14, 2023

Powershell script to add new global rendering to all existing pages

 This is a Bulk update, where you can add a rendering to existing pages with a particular template type.   For new pages, simply add rendering to branch template.

Add-Rendering will add the new rendering to Final Layout or shared Layout. the main trick is -Index 1 will decide the place of the component. Like after Header or After Footer. Let's say if you have 3 components on the final layout and you want to add New rendering at 2nd place. Index should be 1. Index starts with 0.

     Add-Rendering -Item $item -PlaceHolder "header" -Instance $renderingItemWithPlaceholder -Index 1 -Datasource $DatasourcePath.Paths.Path -Device $defaultLayout -FinalLayout


--Complete Script--

$masterdatabase = "master"

#root is the folder

$root = Get-Item -Path (@{$true="$($masterdatabase):\sitecore\content\Home"; $false="$($masterdatabase):\content"}[(Test-Path -Path "$($masterdatabase):\content\home")])

$RenderingItem = Get-Item -Path (@{$true="$($masterdatabase):\sitecore\layout\Renderings\Feature\Components\SubHeader\SubHeader";$false="$($masterdatabase):\content"}[(Test-Path -Path "$($masterdatabase):\content\home")])

$DatasourcePath = Get-Item -Path (@{$true="$($masterdatabase):\sitecore\content\Shared\Datasources\SubHeader\SubHeader";$false="$($masterdatabase):\content"}[(Test-Path -Path "$($masterdatabase):\content\home")])

$placeholderH = "header"

#Find the rendering item and convert to a rendering

$renderingItemWithPlaceholder = $RenderingItem | New-Rendering -Placeholder "header"

$IsRenderingAdded= $true

$count=0

$changedItems=New-Object System.Collections.ArrayList

$props = @{

    Parameters = @(

        @{Name="root"; Title="Choose the report root"; Tooltip="Only items from this root will be returned."; }

        @{Name="RenderingItem"; Title="Choose the Rendering"; Tooltip="Only items from this root will be returned."; }

        @{Name="DatasourcePath"; Title="Choose the Datasource"; Tooltip="Only items from this root will be returned."; }

           )

    Title = "Add Rendering to Article Pages"

    Description = "Choose the criteria for the report."

    Width = 550

    Height = 300

    ShowHints = $true

    Icon = [regex]::Replace($PSScript.Appearance.Icon, "Office", "OfficeWhite", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)

}


$result = Read-Variable @props


if($result -eq "cancel") {

    exit

}


filter Where-Addrendering {

    param(

        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]

        [Sitecore.Data.Items.Item]$item

    )

    

    if ($item) {

$defaultLayout = Get-LayoutDevice "Default"

        #$AllRenderingsInPlaceholder= Get-Rendering -Item $item -Placeholder "*header*" -Device $defaultLayout -FinalLayout

        $sidebarComponentInPlaceholder= Get-Rendering -Item $item -Rendering $RenderingItem -Placeholder "*header*" -Device $defaultLayout -FinalLayout

        #Get-Rendering -Item $item -Placeholder "*header*" | 

        #Foreach-Object 

        #{

           #if($_.ItemId -eq 'your Id') 

            #{

          #$count+=1;

            #}

   if($sidebarComponentInPlaceholder -eq $null){

  # if($IsRenderingAdded){

        Add-Rendering -Item $item -PlaceHolder "header" -Instance $renderingItemWithPlaceholder -Index 1 -Datasource $DatasourcePath.Paths.Path -Device $defaultLayout -FinalLayout

    $count+=1;

    #$IsRenderingAdded=$false

    $changedItem=New-Object PSObject -Property @{

        Name=$item.Name

        ItemPath=$item.Paths.Path

        RenderingName=$RenderingItem.Name

        DatasourcePath=$DatasourcePath.Paths.Path

    }

    $changedItems.Add($changedItem)

   }

        }

    }


$items = @($root) + @(($root.Axes.GetDescendants())) | Where-Object {$_.TemplateName -eq "Article Page"} | Initialize-Item

$finalitems = $items | Where-Addrendering


if($changedItems.Count -eq 0) {

    Show-Alert "Zero Renderings added."

} else {

      #$finalitems = $items | Where-Addrendering

    $props = @{

        Title = "Rendering Added on Items"

        InfoTitle = "Rendering Added on Items  $($count)"

        InfoDescription = "Rendering Added on Items  $($changedItems.Count)"

        PageSize = 25

    }

    

    $changedItems |

        Show-ListView @props -Property @{Label="Icon"; Expression={$_.__Icon} },

            @{Label="Name"; Expression={$_.Name} },

            @{Label="RenderingName"; Expression={$RenderingItem.Name} },

            @{Label="Datasource Path"; Expression={$DatasourcePath.Paths.Path} },

            @{Label="ItemPath"; Expression={$_.ItemPath} }

}


Close-Window


Hope this helps to somebody.