Wednesday, February 18, 2015

Change Insert Options with Sitecore Powershell

This was kind of an odd one to figure out, because it's not very obvious. In the Sitecore content editor UI it's fairly obvious and easy to set.

But what if I needed to set that same option on every single folder in our entire content tree? That could involve tens of thousands of items which is a whole lot of Intern Work.

There is, luckily, an easier way. After checking the Sitecore data content cookbook the field we are looking for is __masters. Like most Sitecore fields, it accepts a pipe delimited list of GUIDs. In our example snippet down below I'm going to retrieve every item under Master:\Content\Home that has the "folder" template and set our insert options on each to be the two items we want.

$allSitecoreFolders = get-childItem -Path "master:\content\home" -Recurse | Where-Object {$_.templatename -eq "Folder"}
foreach($folder in $allSitecoreFolders ){

If you're wondering why, in my examples, I'm casting the results to [void] after EndEdit it's because EndEdit is returning a Boolean value and I'm not terribly interested in watching a lot of scrolling True False in my console window. It'll also prevent all sorts of messy behavior if you are relying on functions to return specific values due to how the terrible way Powershell handles Return keyword from methods.

No comments:

Post a Comment