A Command Line for Sitecore
Two scripting tools for Sitecore, Revolver and PowerShell Extensions, have the power to save significant developer time as demonstrated during the latest New England Sitecore User Group. This article summarizes key insights and takeaways gained during this meetup.
This past Wednesday evening, we were excited to host the New England Sitecore User Group talk on “Sitecore from the Command Line” by veteran Sitecore developer and blogger, Mike Reynolds. Mike presented two scripting tools, Revolver and Sitecore PowerShell Extensions, which have the potential to save significant developer time since they allow for bulk updates of items, rapid refinement of logic, and avoiding the need to build and deploy code for one-time content updates. As Mike stressed, “Saving time saves money!”
Mike presented two tools that give a developer command prompt access into Sitecore. The first was Revolver, developed by Australian Sitecore MVP Alistair Deneys. The tool is now freely available and currently on version 2.1. Mike walked us through the installation process - a simple package install – that results in a new Icon in the Developer menu:
Clicking the Revolver icon opens up this screen on the Sitecore Desktop:
From this starting point, a developer can use Unix/DOS-like commands to navigate the content tree. For example, typing “ls” lists the current children:
master:/sitecore >ls + content + layout + media library + system + templates
From there, you can use “cd” to change context items, and “gf” and “ga” to see the item’s fields and attributes. The navigation is very fast, since no UI processing is involved. But navigation is the tip of the iceberg. Mike showed how to save items in XML format to disk, and rehydrate them in new locations, including rehydrating branches from a single file. Pretty nice stuff! He also showed how to batch commands together into scripts, and how to implement entirely new functionality using C#, such as replacing tokens in content items. The list of Revolver commands is lengthy, so Mike stressed the “help” command to see what’s available.
Next, Mike walked us through Sitecore PowerShell Extensions, a similar scripting project by Polish MVP Adam Najmanowicz. Similarly installed from a Sitecore package, the tool adds two new icons to the Sitecore Start menu: “PowerShell Console” and “PowerShell ISE” (Integraged Scripting Enviornment). Mike recommended the PowerShell ISE option (pictured below), based on its stability and full feature set.
Important points to note:
- The screen is closely integrated into Sitecore’s look and feel, with configurable ribbons, and a Context Item bar that looks like it was borrowed from the Developer Center/XPath builder.
- The commands follow a consistent Verb-Noun syntax, have built-in help, and present the same “all hierarchies are created equal” approach that PS brings to the file system, databases, and the windows registry.
- PowerShell returns Sitecore API objects. When you type “Get-Item”, you are getting a Sitecore Kernel object, with all the power and features of the Sitecore API.
Mike concluded by showing how to build custom commands in PowerShell, demonstrating again with a token substitution feature. The coding process was similar to Revolver - both frameworks provide a BaseCommand class that can be inherited to speed the development process. PowerShell has the additional feature of allowing binding scripts to Content Editor buttons and Context Menus, which opens the door to using these tools to provide author-facing functionality. Mike displayed this by attaching his token replacement program to a right-click option on content items.
A Lively Discussion
Two powerful tools, with similar yet distinct user experiences and features. Which to choose? The audience consensus seemed to be that PowerShell had a leg up because it is built on top of a rich feature set provided by Microsoft and offered better extendibility of the Sitecore UI. My personal take was that Revolver had the edge as an intuitive way of interacting with Sitecore content; I thought I could get more done quicker with Revolver. Mike’s recommendation was to use both tools: Revolver for quick content access and manipulation and PowerShell for scripting new functionality.
The next question we kicked around was where to install this. I think Rick Cabral spoke for many when he said, “There is no way I want this on a client machine.” These are very powerful tools (as the name “Revolver” implies) and should probably be limited to the developer desktop. If you do use these to work with a live site, use extreme caution, remove the DLL when you’re done, and be ready to restore your content from a backup, just in case.
To Learn More
The Revolver package and documentation can be found at http://codeflood.net/revolver. Alistair Deneys has written a number of detailed blog posts on how to use the tool on his blog, “Coffee => Coder => Code” (http://adeneys.wordpress.com/category/net/sitecore/revolver/)
Sitecore PowerShell Extensions can be downloaded from the Sitecore Marketplace and there is an abundant list of documentation resources here: http://blog.najmanowicz.com/sitecore-powershell-console/
Update: Video of the talk is available here: http://www.youtube.com/playlist?list=PLCB3zlgqNwcTi7_DIirILu7b3LhN0ucWY