I’ve received this nice comment on one of my earlier posts and I thought it might warrant a blog post since it is a nice little challenge and might be useful for more scripters in our community.

Hi Adam,

thanks for the pointers on PowerShell! There’s one in particular I’m hoping to get some help with, if that’s ok? I have created a Branch Template in Sitecore, and I want to deploy it under every item in the Content Tree that uses one particular page template (about 200 instances of deployment, in this case). For an added bit of fun, there will be several different language versions being deployed along the way, so I guess that prevents me doing a blanket rollout. Could you please recommend a script that is along the lines of “Add this Branch Template under this Item ID in the following named language codes”? I realise I would then be repeating that line 200 times with varying parent item ID and languages, but I can live with that if that’s the easiest way to do it.

Many thanks for your help!
Phil Neale

First let’s set up a bit of a folder structure for our PoC.

#Clean-up after previous run
Remove-Item master:\content\branch-test -ErrorAction SilentlyContinue -Recurse -Force

#Create a folder strcture for our tests
New-Item master:\content\ -Name "branch-test" -ItemType "Common/Folder"

foreach($i in 1..3){

    #Create folders for items that we will serve as branch hosts
    New-Item master:\content\branch-test -Name "folder $i" -ItemType "Common/Folder" | Out-Null

    foreach($j in 1..3){

        #Create items underneath which we will be creating our branches
        New-Item "master:\content\branch-test\folder $i" -Name "branch-host $j" -ItemType "Sample/Sample Item" | Out-Null

After this the content tree structure should look like on the following picture

Test Item Structure

In the simplest scenario I want to browse the created folder and and under each item of the “Sample Item” template I want to deploy my branch. For the sake of not having to create a branch template (which doesn’t really add anything to this exercise) I’ve picked one of the branches that already exists in vanilla Sitecore 7.1+ by default: /sitecore/templates/Branches/System/Rules/Taxonomy/Element Folder.

#Pull all children from our test folder
Get-ChildItem master:\content\branch-test -Recurse |

    #filter only the items that are of specific template
    Where-Object { $_.TemplateName -eq "Sample Item" } |
    % {
        #Create a branch based on some system branch in Polish language. 
        New-Item $_.ProviderPath -Name "test" -ItemType "Branches/System/Rules/Taxonomy/Element Folder" -Language pl-PL

        #Add Danish, English and Japanese language to the newly created items based on the original Polish version. 
        Add-ItemLanguage -Path "$($_.ProviderPath)\test" -Language pl-PL -TargetLanguage da-DK, en, ja-JP -Recurse

Now I understand that you might not want to do it in all the languages on all of the items so to aid in this we will wrap the branch creation logic in a function:

function Deploy-Branch {
        [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]

        #you can provide your name here as a default if it's constant for all items 
        [string]$Name = "test",
        #you can provide your branch path here as a default if you don't want to do it every time 
        [string]$BranchTemplate = "Branches/System/Rules/Taxonomy/Element Folder",


        #if languages are not defined use the same languages as the item parent
        if($Languages -eq $null){
            $Languages = Get-Item $ParentPath -language * | % { $_.Language.Name } 
        #Create a branch based on some system branch in first language provided. 
        $newItem = New-Item "$ParentPath" -Name $Name -ItemType $BranchTemplate -Language $Languages[0]
        #return the item to the pipeline    

        #If more languages specified - add those to the newly created item branch
        if($Languages.Count -gt 1){
            Add-ItemLanguage -Item $newItem -Language $Languages[0] -TargetLanguage ($Languages | select -Skip 1) -Recurse

And now you can simply enumerate your granular cases (multiple syntax’s as well as language variations used to show the variety of options):

#Use parameters explicitly, create single language item
Deploy-Branch -ParentPath 'master:\content\branch-test\folder 1\branch-host 1' -Name "test 1 language" `
    -BranchTemplate "Branches/System/Rules/Taxonomy/Element Folder" -Languages pl-PL

#Use parameters based on parameter order, create the branch in 4 languages
Deploy-Branch 'master:\content\branch-test\folder 1\branch-host 2'  "test 4 languages" `
    "Branches/System/Rules/Taxonomy/Element Folder" da-DK, pl-PL, en, ja-JP

#create in languages my parent item exists in, use parameter defaults, parent name from pipeline
'master:\content\branch-test\folder 1\branch-host 3' | Deploy-Branch

Hopefully this does the job for you Phil!

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

This entry (Permalink) was posted on Wednesday, August 3rd, 2016 at 2:00 am and is filed under .Net Framework, Code Samples, Open Source, PowerShell, Sitecore, Software Development, Solution, Web applications. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site.

  • Phil Neale

    Hi Adam, thank you very much for this – it looks like this is exactly what I need! I very much like the idea of checking the parent item for the languages to deploy the template in, that is splendid. I can’t pretend to understand the full workings of it yet, but I definitely follow the gist, and I’ll be giving this a try as soon as possible. Again, thank you for taking the time to answer and for explaining the solution! Cheers Adam!

  • Awesome!
    Glad to be of help and thank you for your interest in SPE!

  • Phil Neale

    Hi Adam, thanks again for your help with this, it has really helped!! I have one other question that you might be able to help with please… We have a set of content items that have been exported as XMLs from “Control Panel – Globalization – Export Languages to a file”, mass-translated into 30+ language versions, and are being returned as one XML per language, per item. As there are some 15 items, multiplied by 30 language versions of each, is there a way of importing these files using PowerShell, instead of importing them through “Control Panel – Globalization” one file at a time? Again, many thanks! Phil