Fix Duplicate Item Names in Sitecore

If you are using Sitecore Content Serialization (Sitecore CLI) as the serialization pull and or push command will fail without any meaningful error. Therefore, we must ensure that there are no duplicate item names under same parent ever.

Unexpected HttpResponseMessage with code: BadGateway 
1421 error occurred: 
143 * Status: The command 'powershell -Command 
$ErrorActionPreference = 'Stop';
$ProgressPreference = 'SilentlyContinue'; sitecore ser push' 
returned a non-zero code: 1, Code: 1

Ref: https://doc.sitecore.com/xp/en/developers/100/developer-tools/sitecore-content-serialization-configuration-reference.html

Duplicate item names on the same level are not supported by the CLI, even if such items are under ignored nodes.


Please check my blog post with steps that we can use to avid them in the first place, but in case you already have duplicate item names and your sitecore ser push or sitecore ser pull command is failing without any meaningful message it is important to first check and ensure it is not caused by this duplicate item names under same parent issue.

The first step in this exercise is to identify/find out the duplicates and then we can fix them.

Step 1: Find duplicate item names under same parent

Find duplicates using Sitecore PowerShell command – Slow and expensive

To run this PowerShell command

  1. Login to Sitecore CM with Administrator
  2. On the dashboard click “PowerShell ISEPowerShell ISE
  3. This will open up a PowerShell like window where you can paste this script and click “Execute” in the top ribbon.

Note: This script can be really slow if you are running it on the whole website with good amount of content. Therefore I suggest running it on items where you suspect the problem might be.

The code below is an extension of https://gist.github.com/michaellwest/2ba21ff6e522f265f288d7400ca0dae8

$itemPath = "master:/sitecore/content/Settings/Products"
$rootItem = Get-Item -Path $itemPath
$rootItem.Axes.GetDescendants() | Initialize-Item | 
    Group-Object { $_.ItemPath} | 
    Where-Object { $_.Count -gt 1 } |
    Sort-Object -Property Count -Descending | 
    Select-Object -Property Count, Name

Write-Output "All Done..."

https://github.com/zaheer-tariq/Sitecore-Blog-Gists/blob/main/PowerShell/Find/FindDuplicateItemNamesOnSameLevel.ps1

Run database query to find duplicates – Very Fast but need access to database

You need to have access to the Database and then using SQL Management Studio or any other tool you can run the following query to find duplicates in the entire master database.

This query will give you Parent ID of all items where you have duplicates along with the duplicate item count. You can copy the ID and search it in Content Editor to find the item and then rename the child items where you see duplicates.

SELECT Name, ParentID, COUNT(*) as count
FROM Items GROUP BY Name, ParentID
HAVING COUNT(*) > 1
order by ParentID

UPDATED: New method combines best of two methods above.
Find Duplicate Items Fast Through Sitecore PowerShell

Step 2: Fix duplicate item names under same parent

Manually through Sitecore Content Editor

This is a safe but manual option where you can use Sitecore Content Editor to manually rename items to fix duplicate item names under same parent. You can simply append an incremental number e.g. Item Name 1, Item Name 2 to make them unique.

Through PowerShell Script

If you have quite a few duplicate item names then fixing them manually can be a very hectic task. The Sitecore PowerShell script below can help get the job done quickly.

Get-ChildItem -Path "master:/sitecore/content/Settings/Products" -Recurse | Foreach-Object{
	$id = $_.ID
	$item = Get-Item -Path master: -ID $id
	Write-Output "Checking item $($item.Name)"
	$index = 1
	Get-ChildItem -Path $item.Parent.Paths.FullPath | Where-Object{ $_.ID.Guid -ne $id.Guid -and $_.Name -eq $item.Name } | Foreach-Object {
		$newName = -Join($_.Name, " ", $index)
		Write-Host "$($_.Name) will be renamed to $newName" 
		$_.Editing.BeginEdit()
		$_.Name = $newName
		$_.Editing.EndEdit()
		$index++
	}
}
Write-Output "All Done..."

https://github.com/zaheer-tariq/Sitecore-Blog-Gists/blob/main/PowerShell/Rename/RenameDuplicateItemNamesOnSameLevel.ps1