1

I am using a PowerShell script given below

 $indexName = "sitecore_web_index"
    $criteria = @(
                    @{Filter = "Contains"; Field = "industry_sm"; Value = "{7451A225-6450-4D26-BE04-624FD2E63B76}" },
                    @{Filter = "Contains"; Field = "service_sm"; Value = "{0670EE3C-1D07-4172-9ADB-19127D90C180}" }
                )
     $list = Find-Item -Index $indexName -Criteria $criteria | Initialize-Item

It returns a list of all present language versions per item. Is it possible somehow through the script to make it return only one language version per item (English by default or other if English is not present)?

2 Answers 2

2

To achieve the desired result, the Find-Item function alone won't suffice; additional logic is required.

Here's a working example:

$indexName = "sitecore_web_index"
$criteria = @(
    @{Filter = "Contains"; Field = "industry_sm"; Value = "{7451A225-6450-4D26-BE04-624FD2E63B76}" },
    @{Filter = "Contains"; Field = "service_sm"; Value = "{0670EE3C-1D07-4172-9ADB-19127D90C180}" }
)

# Find items matching the criteria and group them by item ID
$list = Find-Item -Index $indexName -Criteria $criteria | Group-Object -Property ItemId

# Initialize only one language version per item (preferably English)
$filteredList = $list | % { $_.Group | Sort-Object { if ($_.Language -eq "en") { 0 } else { 1 } } | Select-Object -First 1 | Initialize-Item }

# $filteredList now contains one language version per item
foreach($filteredItem in $filteredList){
    Write-Host "Item ID: $($filteredItem.ID), Language: $($filteredItem.Language), Name: $($filteredItem.Name)"
}
0

Try out this code, You can filter content by language using Dynamic LINQ syntax and get results with where a condition for a single language.

$criteria = @(
    @{Filter = "Contains"; Field = "industry_sm"; Value = "{7451A225-6450-4D26-BE04-624FD2E63B76}" },
    @{Filter = "Contains"; Field = "service_sm"; Value = "{0670EE3C-1D07-4172-9ADB-19127D90C180}" }
)

$props = @{
    Index = "sitecore_web_index"
    Criteria = $criteria
    Where = "Language=@0"
    WhereValues = "en"
}

Find-Item @props

Read more details: https://doc.sitecorepowershell.com/appendix/indexing/find-item#example-1

Hope it helps!

1
  • It will only provide en items, there are total 6 languages, if en version not present then i want to get its german version and if german is not present then i want to get french version Commented Aug 12, 2023 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.