Some Useful Sitecore Powershell Scripts Part 1

Hello Everyone. As we all know, when we work with Sitecore, there are many situation where we need to use powershell scripts. It can be for bulk item creation or item modification.

In this blog, we will see some useful powershell script

Creating Bulk Items in Sitecore from one data folder to another.In this example we are creating Country items for example

  • Destination Path (Where item is going to be created)
  • Source Item Path (From where item is going to be fetch)
  • Template to be used : The template path from which item will be created.

       
$destinationPath = "{{Destination Path}}"

$countryItems = Get-ChildItem -Path "{{Source Item Path}}" 


$bulk = New-Object "Sitecore.Data.BulkUpdateContext"
try 
{

 foreach($record in $countryItems)
 {
 
   $item = New-Item -Path $destinationPath -Name $record.Name -ItemType "{{Template to be used}}"
   $item.Editing.BeginEdit()
     $item["Title"] = $record["Title"]
     $item["Value"] = $record["Value"]
   $item.Editing.EndEdit()
 
 Write-Host "Item created: " $record.Name
 
 }
 
}
finally
{
 $bulk.Dispose()
}
	   
 

Creating Bulk Items in Sitecore from CSV.In this example we are creating state items for example

  • Destination Path (Where item is going to be created)
  • Template to be used : The template path from which item will be created.

       
$destinationPath = "{{Destination Path}}"

$importData = Import-CSV "C:\powershell\State.csv"
$bulk = New-Object "Sitecore.Data.BulkUpdateContext"
try 
{

 foreach($record in $importData)
 {
 
  $item = New-Item -Path $destinationPath -Name $record.Title -ItemType "{{Template to be used}}"
  $item.Editing.BeginEdit()
     $item["Title"] = $record.Title
     $item["Value"] = $record.Value
  $item.Editing.EndEdit()
 
 Write-Host "Item created: " $record.Name
 
 }
 
}
finally
{
 $bulk.Dispose()
}
	   
 

Bulk Item template change powershell script for specific Langauge version

  • Source Item Path : Fetch Item from the folder
  • old template ID : the template id of item which need to be changed
  • New Template ID : The new template id to which item should be changed

       
$items = Get-ChildItem -Path "{{Source Item Path}}" -Language "zh-CN"
$bulk = New-Object "Sitecore.Data.BulkUpdateContext"
try{
foreach($item in $items)
    {
        if ($item.TemplateId -eq "{{old template ID}}")
        {
            $item.Editing.BeginEdit();
            $item.TemplateId = "{{New Template ID}}"
            $item.Editing.EndEdit();
            Write-Host "Item id: " $item.ID " - Template id: " $item.TemplateId
        }
    }
}
finally
{
 $bulk.Dispose()
}
	   
 

Fetching Item between a Date Range.

  • Source Item Path (Source path from where items will be fetched)
  • Date Range (the date range value. Example ($_.Date -ge "20200216T000000" -and $_.Date -le "20200617T000000"))

       
# Find the  item between date range
	$props = @{
	   InfoTitle = "Referrers"
		InfoDescription = "Lists all items that are using this item"
		PageSize = 25
	}

$items = Get-ChildItem -Path "{{Source Item Path}}"  -Recurse | 
Where-Object { ($_.Date -ge "{{Date Range}}" -and $_.Date -le "{{Date Range}}")}


$items | Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },
			@{Label="Path"; Expression={$_.ItemPath} },
            @{Label="Id"; Expression={$_.Id} },
            @{Label="Updated"; Expression={$_.__Updated} },
            @{Label="Updated by"; Expression={$_."__Updated by"} },
            @{Label="Created"; Expression={$_.__Created} },
            @{Label="Date"; Expression={$_.Date} }
	   
 

Replace one rendering component with another using Powershell script.

  • rendering path : (rendering item path which will be used for replacing)
  • Sitecore Page Item Path : (Sitecore Item where component will be replaced)
  • rendering param name : (If any rendering paramter available then provide that name for that component)
  • value (Rendering Param Value)

       
# Find the rendering item and convert to a rendering
$renderingPath = "{{rendering path}}"
$renderingItem = Get-Item -Database "master" -Path $renderingPath
$renderingDef = Get-Item -Database "master" -Path $renderingPath | New-Rendering -Placeholder "content"
# Find the item to receive the new rendering
$item = Get-Item master:'{{Sitecore Page Item Path}}'
# Add the rendering to the item
Remove-Rendering -Item $item -Rendering $renderingItem -Device (Get-LayoutDevice "Default")
Add-Rendering -Item $item -PlaceHolder "content" -Instance $renderingDef -Parameter @{ "{{rendering param name}}" = "{{value}}" } -FinalLayout
	   
 

Thanks for reading. Hope this will be helpful in one or another way.Will cover some other useful powershell script in my next part

Happy learning. Happy Sitecoring.

Comments

Popular posts from this blog

Sitecore XM Cloud Form Integration with Azure Function as Webhook

Automate RSS Feed to Sitecore XM Cloud: Logic App, Next.js API & Authoring API Integration

Create and Fetch Content From Sitecore Content Hub One using GraphQL and React