Rule-based Item Insert Option with Sitecore PowerShell Extensions

Sometimes we are caught up with usual ways of doing things that we tend to not learn new ways of doing a specific thing. For instance, one of the first things a Sitecore developer learns is how to insert options and that is as per documentation

Then, there is insert option rules approach to restrict content editors.

The third approach is through Sitecore PowerShell Extensions Integration Points. This is something I haven't used much but seems to be a hybrid between the above two approaches:

Although  SPE documentation is good apart  from examples under /sitecore/system/Modules/PowerShell/Script Library, I thought I would add one example for insert options to bookmark the power of SPE.


Step-by-step:

1. Create PowerShell Script module under a PowerShell Script Module Folder:


Be sure to elevate session before going any further. 

2. Elevate session will allow you to login to Sitecore and then you should activate the module and save:


3. Stick to the hierarchy of examples and create Content Editor and Insert Item of types PowerShell Library:


4. Now, the next step is to create a simple item template:



5. Back in the SPE Script Library, add rules and the necessary script, the id must be same as that you want to insert:

Script Body:

###################################

$parent = Get-Item -Path "."

if($parent) {

    $name = Show-Input -Prompt "Howdy! Enter a name for my item:" -DefaultValue "New My Item"

    if($name) {

        New-Item -Parent $parent -Name $name -ItemType "{662E066A-9D4B-46A7-947E-C8339CC3EB90}"

    }

}

Close-Window

###################################

Power of script and rules in one place:



6. Rebuild All in PowerShell ISE:


I'm unsure if this is necessary for this example since just saving the script reflected the rule in the content tree context menu.

7. Time to test in content editor. Create a structure respecting the rule and end-result seems good:


8. Finally, our custom dialog with Howdy message and default item name we specified:


Although there seems to be a bit of over-work with SPE, not to mention your ability to write rules that too understanding how rules work, this could be a useful option for other relevant and useful scenarios. 

A side-by-side comparison:



Comments

Popular Posts