4

I'm creating multiple item in multiple language in Sitecore using PowerShell Script I'm using 2 script for different different language is this possible in single script. Adding here both script.

First script

$json =ConvertFrom-Json @'
{
"item1": "value in english",
"item2": "value in english"
}
'@  
foreach ($array in @($json.psobject.properties)) {
 $newItem = New-Item -Path "master:/sitecore/content/Home/blogdetail" -Name $array.Name -ItemType "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
write-host "$($array.Name) = $($array.Value)"
 $newItem.Editing.BeginEdit()
   
            $newItem["text"] = $array.Name
            $newItem["title"] = $array.Value
            $newItem.Editing.EndEdit()
} 

Second script

$folders = Get-ChildItem -Recurse -Path "master:/sitecore/content/Home/blogdetail"
$json =ConvertFrom-Json @'
{
"myitem1": "value in arabic", 
"myitem2": "value in arabic"
}
'@
foreach ($path in $folders)
{
    #write-host "$($path.Name)"
    foreach($jsonitem in @($json.psobject.properties))
    {
        #write-host "$($path.Name) and $($jsonitem.Name)"
        if($path.Name -eq ($jsonitem.Name)){
            $newvar= Add-ItemVersion -Item $path -IfExist Append -TargetLanguage "ar-SA"
            $newvar.Editing.BeginEdit()
            $newvar["text"] = $jsonitem.Name
            $newvar["title"] = $jsonitem.Value
            $newvar.Editing.EndEdit()
        }
    }
}

Help me here.

4 Answers 4

4

Yes, You can combine both scripts into a single PowerShell script to create items in multiple languages within Sitecore. Below I'm adding modified version of your script that integrates both functionalities. This single script will first create items in the default language and then add versions for the specified language.

# Create items in the default language
$jsonDefault = ConvertFrom-Json @'
{
   "item1": "value in english",
   "item2": "value in english"
}
'@

foreach ($array in @($jsonDefault.psobject.properties)) {
    $newItem = New-Item -Path "master:/sitecore/content/Home/blogdetail" -Name $array.Name -ItemType "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
    Write-Host "$($array.Name) = $($array.Value)"
    $newItem.Editing.BeginEdit()
    
    $newItem["text"] = $array.Name
    $newItem["title"] = $array.Value
    $newItem.Editing.EndEdit()
}

# Add versions in the Arabic language
$folders = Get-ChildItem -Recurse -Path "master:/sitecore/content/Home/blogdetail"
$jsonFrench = ConvertFrom-Json @'
{
"myitem1": "value in arabic", 
"myitem2": "value in arabic"
}
'@

foreach ($path in $folders) {
    foreach ($jsonitem in @($jsonFrench.psobject.properties)) {
        if ($path.Name -eq $jsonitem.Name) {
            $newvar = Add-ItemVersion -Item $path -IfExist Append -TargetLanguage "ar-SA"
            $newvar.Editing.BeginEdit()
            $newvar["text"] = $jsonitem.Name
            $newvar["title"] = $jsonitem.Value
            $newvar.Editing.EndEdit()
        }
    }
}

Hope this help!! Please let me know if you required any modification.

3

You can use the below scripts -

# Define parameters
$parentItemPath = "/sitecore/content/YourParentItem"  # Change to your parent item's path
$itemTemplate = "{your-template-id}"  # Change to your item's template ID
$itemName = "YourNewItemName"
$languages = @("en", "fr", "de")  # List of language codes

# Get the parent item
$parentItem = Get-Item -Path $parentItemPath

if ($null -eq $parentItem) {
    Write-Host "Parent item not found."
    return
}

foreach ($language in $languages) {
    # Check if the language version exists
    $existingItem = Get-Item -Path "$parentItemPath/$itemName" -Language $language -Version 1
    
    if ($null -eq $existingItem) {
        # Create the new item
        $newItem = New-Item -Path $parentItemPath -Name $itemName -ItemTemplate $itemTemplate -Language $language
        
        if ($null -ne $newItem) {
            Write-Host "Created item '$itemName' in language '$language'."
        } else {
            Write-Host "Failed to create item in language '$language'."
        }
    } else {
        Write-Host "Item '$itemName' already exists in language '$language'."
    }
}
1

Here is the combined PowerShell script to create items in English and then add Arabic versions in Sitecore:

# Define English items
$jsonItems = ConvertFrom-Json @'
{
    "item1": "value in english",
    "item2": "value in english"
}
'@

# Create English items
foreach ($item in $jsonItems.psobject.properties) {
    $itemPath = "master:/sitecore/content/Home/blogdetail/$($item.Name)"
    
    if (-not (Test-Path $itemPath)) {
        $newItem = New-Item -Path "master:/sitecore/content/Home/blogdetail" -Name $item.Name -ItemType "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
        
        $newItem.Editing.BeginEdit()
        $newItem["text"] = $item.Name
        $newItem["title"] = $item.Value
        $newItem.Editing.EndEdit()
    }
}

# Define Arabic items
$jsonArabicItems = ConvertFrom-Json @'
{
    "item1": "value in arabic",
    "item2": "value in arabic"
}
'@

# Create Arabic versions for existing items
$folders = Get-ChildItem -Recurse -Path "master:/sitecore/content/Home/blogdetail"
foreach ($folder in $folders) {
    foreach ($arabicItem in $jsonArabicItems.psobject.properties) {
        if ($folder.Name -eq $arabicItem.Name) {
            $newVersion = Add-ItemVersion -Item $folder -IfExist Append -TargetLanguage "ar-SA"
            $newVersion.Editing.BeginEdit()
            $newVersion["text"] = $arabicItem.Name
            $newVersion["title"] = $arabicItem.Value
            $newVersion.Editing.EndEdit()
        }
    }
}

Check this and let me know if you find any issue.

Hope this helps.

0

You can create the json structure as below that includes details of multiple items in different languages.

$json = @"
[
    [
        {
            "name": "Item1Name",
            "title": "Item1EnglishTitle",
            "text": "Item1EnglishText",
            "language": "en",
            "default": 1
        },
        {
            "name": "Item1Name",
            "title": "Item1ArabicTitle",
            "text": "Item1ArabicText",
            "language": "ar-SA",
            "default": 0
        }
    ], 
    [
        {
            "name": "Item2Name",
            "title": "Item2EnglishTitle",
            "text": "Item2EnglishText",
            "language": "en",
            "default": 1
        },
        {
            "name": "Item2Name",
            "title": "Item2ArabicTitle",
            "text": "Item2ArabicText",
            "language": "ar-SA",
            "default": 0
        }
    ]
]
"@ | ConvertFrom-JSON

In the above json, since en is the language from which we would like to create items, hence for this default property is marked as 1 whereas for other languages it would be 0.

Thereafter using below powershell snippet, items can be created in multiple languages in Sitecore. This script creates the item when the language is default i.e en and for other languages it adds the version of the item.

$folder = "/sitecore/content/Home/blogdetail"

foreach($languageItem in $json)
{
   foreach($languageItemVersion in $languageItem){
      if($languageItemVersion.default){
          $newItem = New-Item -Path "master:$folder" -Name $languageItemVersion.name -ItemType "{2741CC74-B4D8-4CBE-9937-4E1565FB60C1}"
          $newItem.Editing.BeginEdit()
          $newItem["Text"] = $languageItemVersion.text
          $newItem["Title"] = $languageItemVersion.title
          $newItem.Editing.EndEdit()
      }else{
          $newVersion = Add-ItemVersion -Path "master:$folder/$($languageItemVersion.name)" -Language "en" -TargetLanguage "$($languageItemVersion.language)" -IfExist Append
          $newVersion.Editing.BeginEdit()
          $newVersion["Text"] = $languageItemVersion.text
          $newVersion["Title"] = $languageItemVersion.title
          $newVersion.Editing.EndEdit()
      }
   }
}

Using above changes, you will be able to successfully create items in multiple languages using single powershell snippet. This will be a generic script that can run for multiple languages in future without changing any code.

Hope this helps!!! Let me know in case you face any issues.

5
  • this is not good approach for me. I don't want to change json this is to much complex process for me there is 500 items.
    – Kiran
    Commented Oct 10 at 5:07
  • @Kiran I made those separate variable for title and text since currently you are using value and name to fill title and text field..Will that be the case for you..In that case the json structure will be reduced.
    – ckhanna
    Commented Oct 10 at 5:17
  • Secondly I noticed since you are using item.name field in the text field, that field wont be translated in the arabic language.
    – ckhanna
    Commented Oct 10 at 5:25
  • I have checked your solution not working
    – Kiran
    Commented Oct 10 at 5:56
  • @Kiran I have tested it at my end. Can you please let me know the issue you are facing.
    – ckhanna
    Commented Oct 10 at 6:00

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.