Skip to content

Instantly share code, notes, and snippets.

@keyan1603
Created September 20, 2023 18:33
Show Gist options
  • Save keyan1603/895e8344ecaff7d74e7fccb08cfae7d0 to your computer and use it in GitHub Desktop.
Save keyan1603/895e8344ecaff7d74e7fccb08cfae7d0 to your computer and use it in GitHub Desktop.
Update fields in Sitecore for all items based on a specific Template in all languages using Powershell script – with Progress bar
#Get all Product from the Master index for all languages.
#You can change this to get data from specific language codes
#like 'en', 'fr', etc.
$searchCriteria = @(
@{ Filter = "Equals"; Field = "_language"; Value = "*"; }
@{ Filter = "Equals"; Field = "_templatename"; Value = "Product"; }
)
#This script executes against Sitecore master index
$searchProps = @{
Index = "sitecore_master_index";
Criteria = $searchCriteria
}
#You can uncomment certain sections here to test your code
#on just 10 items.
#Use Expand "Fields" to see all the field values stored in the Sitecore Index.
$products = Find-Item @searchProps #-First 10 #| Select-Object -Expand "Fields"
#Total number of Product
Write-Host "Product Count" $products.Count
$Stats = @{
Total = $products.Count;
ProcessCounter = 1;
Success = 0;
NotFound = 0;
}
#Event disabler to make sure no events are triggered
#You can avoid this if not required
New-UsingBlock (New-Object Sitecore.Data.Events.EventDisabler) {
$products | ForEach-Object {
$productItem = $_ | Initialize-Item
$productTitle = $productItem.Fields["ProductTitle"].Value
$Stats.Success++
if($productTitle -ne $null -AND $productTitle.IndexOf("Karthik", 0, $productTitle.Length - 1) -ge 0)
{
Write-Host "Updating Product Title for item '$($productItem.Name) - $($productItem.DisplayName)'. ( $($Stats.Success) / $($Stats.Total))"
$updatedProductTitle = $productTitle -replace "Karthik", "Keyan"
$productItem.Editing.BeginEdit() | Out-Null
$productItem.Fields["ProductTitle"].Value = $updatedProductTitle
$productItem.Editing.EndEdit() | Out-Null
}
$percent = [int](($Stats.ProcessCounter * 100) / $Stats.Total)
Write-Progress -Activity "Updating Product Title " -Status "$percent% Complete:" -PercentComplete $percent;
$Stats.ProcessCounter++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment