In the he latest update of PowerShell Console for Sitecore, I’ve started experimenting with further integration – context scripts. What is that and what can it do for you?

You can safely assume PowerShell is not something your regular users will aspire to learn, but it does not mean they cannot harness its power if you enable them to.

If you look closely at the latest Microsoft solutions you may notice that a lot of what GUI does is running some scripting behind the scenes. This is true for a long time with the SQL Server Management studio where pretty much for any action you can generate a script that will let you automate a common task.

SqlManagementScripting

Similarly but the other way around, with the PowerShell Console for Sitecore, you can attach scripts to the context menu in your item tree.

ContextScripts

An execution of such script can be completely silent but you can also add a question before a script is executed:

ContextScriptsConfirmation

and  if needed – show the results:

ContextScriptsResults

Anatomy of a context script

The PowerShell console comes with 2 sample scripts which for better or worse only demonstrate the potential of what can be done. The context part means that before executing your script the PowerShell environment location is set to your content item so Get-Item should yield the item you clicked on, whereas Get-ChildItem will get all of its children, potentially with their children if  -recurse is used.

1. Create a script

Add a new script using the /sitecore/templates/Cognifide/PowerShell Script template in the core database. In my case I store my scripts within the /sitecore/content/Applications/PowerShell Console/Scripts branch but anywhere in the core database is fine really.

  • Fill in the Script body part with your script.
  • If you want a warning/confirmation request to popup prior to the script execution provide it in the “pre-execution warning” box
  • and tick the checkbox if your script can generate output or you expect it to occasionally show errors.

ContextScriptBody

2. Create a menu item for your script

Create a context menu item of template /sitecore/templates/System/Menus/Menu item within the /sitecore/content/Applications/Content Editor/Context Menues/Default/PowerShell/ branch in the core database. If you have more than just a few scripts, a more elaborate structure might need to be created to group your scripts by functionality/purpose.

3. Bind it to your script to the menu item

Within the menu item you’ve just created edit the Message field and bind the script to it using the following pattern:

item:executescript(id=$Target,script={1680E211-BD28-49BE-82FB-DA7232814C62},scriptDb=core)

where the script guid is the ID of your script and the scriptDb is the database the script is located in.

Since this is a simple Sitecore command – similarly you can use the PowerShell functionality to expose your script on any ribbon or other kinds of menus!

That’s it really, and since you have pretty much all of the Sitecore API at your disposal you can hotfix a great deal of missing functionality without ever logging into Visual Studio, how is that not cool?!

On a final note… you might want to protect scripts that might create a heavy load on your site or do anything critical on your database from some users, but that’s a matter of simple permissions setting on your menu items.

[Download & Enjoy]

All feedback appreciated!

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...



This entry (Permalink) was posted on Tuesday, November 22nd, 2011 at 2:34 pm and is filed under Uncategorized. 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.