Sitecore PowerShell – Enable Item Level Language Fallback

The use case for this blog is to enable item-level fallback on all items based on any user-defined template (or based on any template located in a given folder).

Here are three steps you need to follow in order to set up the language fallback functionality in Sitecore:

  1. Enable site-level fallback in \Website\App_Config\Include\Sitecore.LanguageFallback.config.
  2. Specify the Fallback Language for the language items located in sitecore/system/Languages.
  3. Either enable item-level fallback or field-level fallback per your need.

To enable fallback for all items of a given template, just set it on the template’s __Standard Values.

fallback

However, when there are too many templates to deal with, this task can become very tedious, so we are going to automate this process and perform a batch update using Sitecore Powershell Extensions.

Here is my script:

Write-Host "Creating Standard Values items for:" -ForegroundColor green
$Items = Get-ChildItem master: -ID "{GUID of the templates folder}" -Recurse | Where-Object {$_.TemplateName -eq 'Template'-and `
                !$_.Children["__Standard Values"] }

foreach($Item in $Items)
{
    Write-Host $Item.Paths.Path
    $standardvalues = New-Item -Parent $Item -Name "__Standard Values" -type $Item.ID
    $Item.Editing.BeginEdit()
    $Item["__Standard values"] = $standardvalues.ID
    $Item.Editing.EndEdit()
}
Write-Host "Total number of items modified: " $Items.Count

Write-Host "Enabling Item Level Fallback for:" -ForegroundColor green
$Items = Get-ChildItem master: -ID "{ GUID of the templates folder }" -Recurse | Where-Object {$_.Name -eq '__Standard Values'-and `
                $_.'__Enable item fallback' -eq $false }
foreach($Item in $Items)
{
    $Item.'__Enable item fallback' = $true
    $Item.Paths.Path
}
Write-Host "Total number of items modified: " $Items.Count

The first half of the script finds all templates within the given folder (such as a “User Defined Templates” folder), and creates a __Standard Values item for the templates that don’t already have one. The second half of the script goes through all the __Standard Values items that don’t have Enable Item Fallback checked and set them to true.

The paths of all modified items are printed to the console:

Creating Standard Values items for:
 /sitecore/templates/User Defined/Public/Carousels/FeaturedItemsCarousel
 True
 /sitecore/templates/User Defined/Public/Carousels/RecognitionCarousel
 True
 /sitecore/templates/User Defined/Public/Carousels/TimelineCarousel
 True
  …
Total number of items modified:  39


Enabling Item Level Fallback for:
 /sitecore/templates/User Defined/Public/Careers/Statement Block/__Standard Values
 /sitecore/templates/User Defined/Public/Careers/Header Section/__Standard Values
 /sitecore/templates/User Defined/Public/Careers/Landing Hero Section/__Standard Values
  …
Total number of items modified:  227

 

Now you efficiently have all items based on the templates in that folder enabled for fallback. Please feel free to let me know your thoughts and suggestions. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s