Sitecore PowerShell – Practical Scripts, Part 1

powershell

I have been working a lot lately with the Sitecore PowerShell module. Our site is international and therefore we need to support lots of different languages, and the PowerShell module is an invaluable tool in maintaining content across cultures.

There are lots of great resources available to learn more about the module.
Here I am going to start compiling a repository of (hopefully) useful, practical scripts that you can borrow, steal, and learn from. Here we go…

Quick syntax note: in most of the examples below, I use the following syntax for selecting items:

Get-Item -Path "master:/sitecore/content/Home/About Us"

Technically, the “/sitecore” part is not necessary – PowerShell assumes that part. But when copy/pasting out of Sitecore, I’ve found it’s a lot easier to just paste in the entire path rather than go back and remove that one little piece. It really doesn’t matter. Of course you can also use the -ID for selecting, but again, I’m so used to just copy/pasting the path that it’s just easier for me. Do what you like 🙂

Adding Item Languages

These are probably the most-used scripts in my arsenal. We are forever needing to copy/create new language versions for items and child items.
(NOTE: we don’t use language fallbacks much, mostly for compliance and legal reasons, and because they have caused us too many headaches in the past. Therefore managing separate versions for each language/country is an essential and time-consuming task. These scripts help.)

Add Item Language – Single Item

As the title suggests, this simply takes a single item and creates new language versions of that item, copied from a single version. The “-Language” value is the one we’re copying from, and the “-TargetLanguage” is the list we’re copying to. The “-TargetLanguage” value can be a list or a single string.

Get-Item -Path "master:/sitecore/content/Home/About Us" | 
    Add-ItemLanguage -Language "en-US" -TargetLanguage @("en-CA","fr-CA","de-DE") -IfExist Skip

Add Item Language – Child Items

Same as above, but this time we’re creating new language versions for all child items of an item. Very helpful for bulk operations like setting up a new country/language.
NOTE: this does NOT copy the parent item, so if you’re doing bulk operations, don’t forget the parent!

Get-ChildItem "master:/sitecore/content/Home/About Us" -Recurse | 
    Add-ItemLanguage -Language "en-US" -TargetLanguage @("en-CA","fr-CA","de-DE") -IfExist Skip

Updating Fields

Like language fallbacks, we don’t use shared fields all that much either – there are always exceptions for a handful of countries that make shared fields an impossibility. So often we need to update field values across multiple language versions. Here are some useful update scripts:

Set String/Text Value

Updates a single text value.
Another syntax note: as in most programming languages, there are many ways to write a particular script. The “foreach” loop makes the most sense to my C# brain, so that is the syntax I use most often. If you want to experiment with other ways, go for it.

# get all child "Product Item" templates, all languages
$itemList = Get-ChildItem -Path "master:/sitecore/content/Home/Products" -Language * -Recurse | 
    Where-Object { $_.TemplateName -eq "Product Item" }
    
foreach ($item in $itemList)
{
    # check to make sure we need to update the field
    if ($item."Product Type" -ne "widget")
    {
        $item.Editing.BeginEdit()
        $item."Product Type" = "widget"
        $item.Editing.EndEdit()
    }
}

Set Boolean Value

Updates a single boolean/checkbox value.

# get single item, all languages
$items = Get-Item -Path "master:/sitecore/content/Home/Products" -Language *
foreach ($item in $items)
{
    # set Right Align Text to true/checked
    $item."Right Align Text" = [int]1
    # set Full Screen to false/unchecked
    $item."Full Screen" = [int]0
}

Set Workflow Values

Updates workflow fields for a specific media item

# get the Sitecore IDs for the workflow and the approved status
$workflow = "{1B497ED4-6132-4BDF-8AB8-07890AF0DF8A}"
$approved = "{5E50B26D-6344-4167-9CFD-9C7898458EE3}"

# get all child items, ignoring media folders
$items = Get-ChildItem -Path "master:/media library/widget images" -Language * -Recurse | 
    Where-Object { $_.TemplateName -ne "Media folder" }
$items.Editing.BeginEdit()
foreach ($item in $items)
{
    # set all workflow fields as needed
    $item.__Workflow = $workflow
    $item."__Workflow state" = $approved
    $item.__Lock = ""
    $item."__Default workflow" = $workflow
}
$items.Editing.EndEdit()

So that’s it for Part 1. There are lots more to come, so stay tuned.

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s