Created
July 26, 2019 07:25
-
-
Save tackme31/9e2af2a3620e83f73c73eddf778e168b to your computer and use it in GitHub Desktop.
A Sitecore PowerShell Extensions script for comparison the fields of items.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$resultTypes = [ordered]@{ | |
"Show all" = 1 | |
"Only matched" = 2 | |
"Only unmatched" = 4 | |
} | |
function Get-ItemTitle { | |
param ( | |
[Sitecore.Data.Items.Item]$Item | |
) | |
$displayName = $Item.DisplayName | |
$databaseName = $Item.Database.Name | |
$languageName = $Item.Language.Name | |
$id = $Item.ID.Guid.ToString("D").SubString(0, 8) | |
return "$displayName ($databaseName, $languageName, $id)" | |
} | |
function Get-Comparisons { | |
param ( | |
[Sitecore.Data.Items.Item[]]$Items, | |
[switch]$UseNameForFieldIdentifier | |
) | |
$fieldIdentifier = $(if ($UseNameForFieldIdentifier) {"Name"} else {"ID"}) | |
$fields = @() | |
$Items | % { $_.Template.Fields | % { $fields += $_ } } | |
$fields = $fields | Group-Object "$fieldIdentifier" | % { $_.Group | Select-Object -First 1 } | |
$result = @() | |
$fields | % { | |
$field = $_ | |
$itemFields = $items | % { @{Title=$(Get-ItemTitle $_); FieldValue=$_[$field."$fieldIdentifier"]} } | |
$row = [PSCustomObject]@{ | |
Matched = $($itemFields | % { $_.FieldValue } | Select-Object -Unique).Count -eq 1 | |
"Field Name" = $field.Name | |
} | |
$itemFields | % { $row | Add-Member $_.Title $_.FieldValue } | |
$result += $row | |
} | |
return $result | |
} | |
function Show-ComparisonView { | |
param ( | |
[Sitecore.Data.Items.Item[]]$Items, | |
[Int]$ResultType, | |
[switch]$IncludeStandardFields, | |
[switch]$UseNameForFieldIdentifier | |
) | |
$iconProp = @{ | |
Name="Icon" | |
Expression={ | |
if($_.Matched) { | |
return "office/16x16/check.png" | |
} | |
else { | |
return "office/16x16/delete.png" | |
} | |
} | |
} | |
Get-Comparisons $Items -UseNameForFieldIdentifier:$UseNameForFieldIdentifier ` | |
| Where-Object { $(if ($IncludeStandardFields) { $true } else { -not $_."Field Name".StartsWith("__") }) } ` | |
| Where-Object { $(if ($ResultType -eq $resultTypes."Only matched") { $_.Matched } else { $true }) } ` | |
| Where-Object { $(if ($ResultType -eq $resultTypes."Only unmatched") { -not $_.Matched } else { $true }) } ` | |
| Select-Object *,$iconProp -ExcludeProperty Matched ` | |
| Show-ListView -Title "Item Comparison Result" | |
} | |
$params = @( | |
@{ Name = "items"; Title = "Select the items to compare"; Value = $SitecoreContextItem; Editor = "treelist"; Source="DatabaseName=$($SitecoreContextItem.Database.Name)&AllowMultipleSelection=no" }, | |
@{ Name = "resultType"; Title = "Select the result type"; Value = 1; Options=$resultTypes; Editor = "combo" }, | |
@{ Name = "includeStandardFields"; Title = "Include the standard fields"; Value = $false; Editor = "checkbox"; Columns = 6 }, | |
@{ Name = "useNameForFieldIdentifier"; Title = "Use the name for field identifier"; Value = $false; Editor = "checkbox"; Columns = 6 } | |
) | |
$result = Read-Variable -Parameters $params -Title "Item Comparison Settings" -Description "Set the items and the options for comparison." | |
if ($result -ne "ok") { | |
Exit | |
} | |
if ($items.Length -lt 2) { | |
Show-Alert "Select two or more items to compare." | |
Exit | |
} | |
Show-ComparisonView $items $resultType -IncludeStandardFields:$includeStandardFields -UseNameForFieldIdentifier:$useNameForFieldIdentifier |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment