Saturday, March 24, 2018

Sitecore Powershell:: Dictionary Audit Series -2


Sitecore Dictionary Duplicate Keys

One of the major issue in the Sitecore Dictionary is Duplicate keys. For instance, if we have key called “optional” and we have created many dictionary items for the key “optional”. Even though we create many dictionary items for the same key, when we call Sitecore.Globalization.Translate.Text("optional"), this will return the firstordefault dictionary items in the dictionary tree.

Same key “optional”
  • Dictionary item 1-Translation:  This is optional text1
  • Dictionary item 2-Translation: This is optional text2
  • Dictionary item 3-Translation: This is optional text3

In the above example, every time and in all the place wherever we use “optional” dictionary key, this will show “This is optional text1”. Even though we change the text in Dictionary item 2 and 3, it will not reflect. This issue will become major if we have multilingual project, which means we will change the translation in one item but it will not get reflect in the page due to duplicate keys.

So in order to resolve this issue, I have written the Sitecore Powershell script to identify the duplicate keys.
As it is continuation of My previous blog, I have used the same fields like dictionary keys and the translate against each language in the our website and in addition to that I have added the Dictionary item id.

Script:
$root = Get-Item -Path (@{$true="master:\system\Dictionary\"; $false="master:\system\"}[(Test-Path -Path master:\system\Dictionary)])
$props = @{
        InfoTitle = "Dictionary Items"
        InfoDescription = "Lists of Dictionary Items"
        PageSize = 250
        Title = "Dictionary Items Report"
    }
$result = Read-Variable -Parameters `
    @{Name="root"; title="Items under"; Tooltip="choose the below path for searching dictionary items"} `
    -Description "This dialog shows search path of dictionary items" `
    -Title "Search Dictionary" -Width 500 -Height 300 -ShowHints

if($result -eq "cancel"){
    exit;
}
$items= [Sitecore.Data.Managers.LanguageManager]::GetLanguages([Sitecore.Data.Database]::GetDatabase("web"))

$reportItems = @()
$dictionaryitems = Get-ChildItem  -Path $root.ProviderPath -Recurse | Where-Object { $_.TemplateName -match "Dictionary entry"  }
if($dictionaryitems.Count -eq 0) {
    Show-Alert "There are no dictionaryitems"
} else {
foreach($dictionaryitem in $dictionaryitems){
$h=@{}
$h.add("ID",$dictionaryitem.Id)
$h.add("Key",$dictionaryitem.Fields["Key"])
foreach($item in $items){
if($dictionaryitem.Database.GetItem($dictionaryitem.ID,$item) -ne $Null ){
$h.add($item.Name,$dictionaryitem.Database.GetItem($dictionaryitem.ID,$item).Fields["Phrase"])
}
}
$reportItem = [pscustomobject]$h
$reportItems += $reportItem
}

$properties = $reportItems[0].PSObject.Properties | foreach-object {$_.Name}


$groupresult =$reportItems | Group-Object -Property Key | Where {$_.Count -gt 1}
if($groupresult.Count -gt 0) {
$groupresult.Group | Show-ListView -Property $properties
}
else
{
Show-Alert "There are no Duplicate keys"
}
}
Close-Window

How it works:

 Step 1:


 Step 2:

Step 3:

Step 4:



  
 Download the Sitecore package: Sitecore Dictionary Duplicate Keys

No comments:

Post a Comment