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
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 { [CmdletBinding()] param( [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] [string]$ParentPath, #you can provide your name here as a default if it's constant for all items [Parameter(Position=1)] [string]$Name = "test", #you can provide your branch path here as a default if you don't want to do it every time [Parameter(Position=2)] [string]$BranchTemplate = "Branches/System/Rules/Taxonomy/Element Folder", [Parameter(Position=3)] [string[]]$Languages ) #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 $newItem #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!
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.