Tag Archives: Sitecore PowerShell Extensions

5 Steps To Content Editor Ribbon Buttons Using Sitecore Powershell Extensions

I have been playing around with Sitecore PowerShell Extensions now for about week or so and love the module. If you haven’t had an opportunity to really dive into SPE, I highly recommend you take a little time to learn a little bit about it as it will really open your eyes on how to get things done in Sitecore fast and efficiently. There is a bit of learning curve, but the reward is worth it when you see just how quickly you can get things done with little code once you get the hang of things. Here is a link to the SPE module on the Sitecore Marketplace:

https://marketplace.sitecore.net/Modules/S/Sitecore_PowerShell_console

For this blog post, I am going to be doing a walk-through of a basic Language Version Utility. I will show how to create a button in the ribbon that when clicked upon will show a dialog box with some instructions and a drop-down list, then allow the user to select a value from the drop-down list, and then and then execute a script passing in the value from the drop-down list as a parameter. All using Sitecore PowerShell Extensions and not one bit of C# code folks! Yes, I was blown away by this as well! You don’t even have to create the buttons in the ribbon the traditional way in the Core database as you typically needed to. SPE does it all for you!

Okay, enough of my excitedness (might not be a word, but I’m excited) on this module and let’s get down to business.

Step 1: I started with downloading the module and installing it. I know you got that part covered so moving on.

Step 2: Next, I right-clicked the Script Library item at /sitecore/System/Modules/Script Library and then Insert–>Module. I named it Language Version Utility and then clicked on the check box for Content Editor Ribbon then clicked Proceed. After a little bit, the script finishes and I have a new PowerShell Script Module called Language Version Utility in the Script Library. That looks something like this:

New Module Called Language Version Utility

New Module Called Language Version Utility

Step 3: For this utility I wanted to put a couple of buttons in the Versions tab of the Contextual Ribbon and group them in a chunk. Using SPE, all I had to do was create a new PowerShell Script Library under Versions, which I named “Copy” and then I simply added to 2 PowerShell Scripts called “Copy To Version” and “Create Versions” as children of “Copy” as such:

New Copy Chunk In Versions Tab Of Contextual Ribbon

New Copy Chunk In Versions Tab Of Contextual Ribbon

Step 4: I just picked some icons that I think would be snazzy for the PowerShell Scripts, “Copy To Version” and “Create Versions”, and then on to a super cool part. I simply opened up the PowerShell ISE in the Development Tools of the Sitecore Admin Desktop as seen below:

PowerShell ISE In Sitecore Desktop Development Tools

PowerShell ISE In Sitecore Desktop Development Tools

Then I clicked on the Settings tab, and clicked on the Rebuild All drop-down, and then clicked the option for Sync Library with Content Editor Ribbon. A script will run, and voila now I have a new chunk in the Versions Tab of the Contextual Ribbon called “Copy” with 2 Large buttons called “Create Versions” and “Copy To Version”:

Copy Chunk In Versions Tab Of Contextual Ribbon

Copy Chunk In Versions Tab Of Contextual Ribbon

Step 5: Now it’s time to start creating my script I want to run when I click either of the buttons. For this example, I will only discuss the “Create Versions” button. So all I have to do is right-click on the “Create Versions” PowerShell Script item and then click on “Edit with ISE” as seen below:

Edit With ISE

Edit With ISE

That will take me right into the PowerShell ISE so I can start creating my script I want to run when I click the “Create Versions” button.

The scripting itself is out of scope on this post, as you will need to familiarize yourself with the scripting using the manual to really start understanding how to script. However, I will post up my script that I used and give you some pointers on what I did to show you. Keep in mind that there are many examples that also come with the module as well. Look for what you can in the examples, repurpose, extend, and ask questions to those in the Slack channel for “module-spe” and you will be sure to receive some help from some Sitecore folks. We are all here to help!

Anyways, here is my script that runs when I click the “Create Versions” button:

$result = Read-Variable -Parameters `
 @{ Name = "languageItem"; Title="Choose A Language"; 
 Source="DataSource=/sitecore/system/Languages"; 
 editor="droplist"} `
 -Description "This script will recursively create language versions for each item under the currently selected node in the content tree using the selected language below. Make sure the correct node is selected now in the content tree. If not, cancel and choose your node in the content tree and try again." `
 -Title "Create Versions Script" -Width 400 -Height 200 -OkButtonName "Create Versions" -CancelButtonName "Cancel" -ShowHints

if($result -ne "ok")
{
 Exit
} 

Get-ChildItem -Recurse . | Add-ItemLanguage -TargetLanguage $languageItem.Name

Basically, I am creating a variable called $result that is taking a result from the Read-Variable cmdlet. Keep your eye on the Name parameter as we will use $languageItem later in the script as that is where the value will be stored when the user chooses a language. This can be a bit confusing at first. You will notice there is also a title, description, and some specs on the dialog box. But once, we get to the bottom, that’s where the important part is. I am basically getting the child items recursively of the item that was selected in the content tree, then adding a new version to each of the child items in the selected language from the user input, which in this case is Spanish. In order to create the version I needed the language name, which I got from the $languageItem.Name. Once this script is saved (after thorough testing in PowerShell ISE to ensure its working), we can try it out.  Let’s take a look at what happens now when I click on the “Create Versions” button using my selected item in the content tree and using a new language I added for Spanish:

Selected Item In Content Tree With No Child Spanish Versions

Selected Item In Content Tree With No Child Spanish Versions

Script Output For Create Versions

Script Output For Create Versions

Now, for those wondering, I could just have well put in a droptree in here to select a content tree item instead of having the user choose an item from the content tree and then click the button. However, I felt this might be a better user experience and a non-duplication of efforts. Also, that type of utility might be found in the Control Panel. But, remember…with SPE you can take advantage of all that and so much is possible! So experiment with what you feel works for the client!

Anyways, I choose the Spanish language and click on Create Versions and my script runs and when it’s done I now have a Spanish version for “Sample Item CA” and “Sample Item CB”.

New Spanish Version of Sample Item CA

New Spanish Version of Sample Item CA

Again, all this was done with no C# code at all! SPE is awesome, and I am sold on the efficiency and ease of use now after diving in first-hand and gaining some knowledge. I will be blogging on the module much more as I gain more familiarization with more advanced topics. Happy coding!