Challenges in getting Sitecore item which has hyphen or dash(-) in the name
Let's imagine a scenario where we want to extract all the item details from a Sitecore based on the list of page URLs provided in the CSV file.
The Url format in the CSV file:
https://{domainname}/{language}/{path-with hypen}
So we can think of using Powershell Get-Item to get all the details of URLs from the given CSV file.
we expected that all would go smoothly, but when we saw the result most of the URLs got failed by throwing the item was not found exception in the PowerShell, we thought that the issue was because of the URL provided in the CSV which has a hyphen in it(replaced all the spaces in the item name with a hyphen), it is because of the following settings.
<
encodeNameReplacements
>
<
replace
mode
=
"on"
find
=
"&"
replaceWith
=
",-a-,"
/>
<
replace
mode
=
"on"
find
=
"?"
replaceWith
=
",-q-,"
/>
<
replace
mode
=
"on"
find
=
"/"
replaceWith
=
",-s-,"
/>
<
replace
mode
=
"on"
find
=
"*"
replaceWith
=
",-w-,"
/>
<
replace
mode
=
"on"
find
=
"."
replaceWith
=
",-d-,"
/>
<
replace
mode
=
"on"
find
=
":"
replaceWith
=
",-c-,"
/>
<
replace
mode
=
"on"
find
=
" "
replaceWith
=
"-"
/>
<!-- to replace space with dash -->
<
replace
mode
=
"on"
find
=
"_"
replaceWith
=
"-"
/>
<!-- to replace underscore with dash -->
</
encodeNameReplacements
>
we verified again the failed items from Sitecore, we come to know that some item names in cms itself were created with hyphens in some parts of the words.
#function to get the item from indexfunction getSitecoreItem($itemUrl){# get the item name from the url$pageUrl =$itemUrl$uri = [System.Uri]::new($itemUrl)$lastPart = $uri.Segments[-1]# Get the language code from the url$regex = [regex]"/([a-z]{2}(?:-[A-Z]{2})?)/"$matches = $regex.Match($itemUrl)if ($matches.Success) {$languageCode = $matches.Groups[1].ValueWrite-Output "Language Code: $languageCode"} else {Write-Output "Language Code not found"}# Define the item name you want to search for$itemName = $lastPart# Define the Sitecore index to query (master index in this example)$indexName = "sitecore_master_index"# Build the search query$searchString = "*$itemName*"# Execute the search query against the specified index$searchResults = Find-Item -Index $indexName -Criteria @{Filter = "Contains"; Field = "_name"; Value = $searchString}# Check if any results were foundif ($searchResults -ne $null) {$results = [PSCustomObject]@{"ItemId" = $searchResults.ItemId"LanguageCode" = $languageCode}return $results} else {return $nullWrite-Host "Item not found with name: $itemName"}}#Start processing the CSV file to get the item details# csv file path$csvPath = "d:\\pageurls.csv"$csvData = Import-Csv -Path $csvPath$itemsNotAvailableArray = @()$itemsArray = @()foreach ( $row in $csvData ){# Get the column name Path from the csv which has the url$itemPath = $row.Path# Get the item from sitecore from the url$itemfromindex = getSitecoreItem($itemPath)if($itemfromindex -ne $null){$itemversions = Get-Item -Path master: -ID $itemfromindex.ItemId -Language $itemfromindex.LanguageCodeWrite-Host $itemfromindex.ItemIdWrite-Host $itemfromindex.LanguageCode}else{$itemversions = $null}$items = $itemversionsif ( $itemversions -eq $null) {$itemnotavailable = [PSCustomObject]@{"ItemPath" = $row.Path}$itemsNotAvailableArray += $itemnotavailablewrite-host $row.Path}if ( $itemversions -ne $null) {foreach ($item in $itemversions) {# Create a custom object with desired item fields$itemAvailable = [PSCustomObject]@{"ItemPath" = $item.Paths.Path"ItemName" = $item.Name"Title" = $item["Title"]"Summary" = $item["Summary"]"ItemTemplateName" = $item.TemplateName"ItemLanguage" = $item.Language.Name"ItemVersion" = $item.Version.Number}$itemsArray += $itemAvailablewrite-host $item.Paths.Path}}}$itemsArray | Export-Csv -Path "D:\latest_published_items.csv" -NoTypeInformation -Encoding UTF8$itemsNotAvailableArray | Export-Csv -Path "D:\items_not_available.csv" -NoTypeInformation -Encoding UTF8Write-Host "Export completed successfully."
Comments
Post a Comment