powershellsitecore

Why changing template in Sitecore using powershell script creates a version in English language even if there no version exists?


Am running the powershell script for China site, so basically all the items will have Chinese version but dont have english version.We are changing certain templates with new template for this site and while doing this a version is getting created by default in english language even if the version is 0 in English language before running the script. So after the script got executed a version is added in English. How its happening and how to prevent it.

Am basically using the below script:

$item.Editing.BeginEdit(); $item.TemplateId = $newTemplateId $item.Editing.EndEdit();

Need to prevent a default unwanted version creation in english language.


Solution

  • This is a common issue/mistake when working with Sitecore items using Sitecore PowerShellExtensions (SPE), that doesn't have any language version in the DefaultContentLangage. The issue is that when you run your SPE script, it runs on the default language as context language. A new language version is also created, if none exists, when you perform the Editing.BeginEdit() / .EndEdit().

    Before editing an item, ensure the script has the context language. If you're running in the ISE, you can set it in the menu. If you want to set it in code, you can set it via [Sitecore.Context]::SetLanguage($language, $false)

    Also note that it may not be a very good idea to set the TemplateId property when changing an item template. Use the item.ChangeTemplate() method instead, or even better in the context of PowerShell, use the Set-ItemTemplate cmdlet.