Adding your own PS code (snippet) to Sitecore PowerShell Snippets Plugin

While I was playing with scripts in Sitecore PowerShell Extensions, I thought how good and handy it would be to have a comment/uncomment line(s) option in the SPE editor window similar to Visual Studio. So, as I started thinking in those lines, I was looking at options and what I landed might not be perfect but a good start for a weekend exercise! So, I created a code snippet that will just add a # to the code window. Might not be perfect but good that I could explore a couple of other things here! 

Note that this insert code snippet option could also be used to store on-demand scripts for content editors:  Some regular house-keeping scenarios where you write scripts and hand-over the scripts to the editors to execute as they need - These are also probably scenarios wherein previously you would think of launching SPE from launchpad icon! Now, you can configure the script and allow them to select the configured option from this SPE plugin and enjoy more autonomy!

Anyway, first things first, here is what I did as a hack for commenting a single line in the SPE Script Window:

The PowerShell Plugins ribbon has a handy option to Insert Code Snippets:


This option has some useful snippets like these:



So, if you click something like if-else, you get sample code added to the code window:



Next, if you track where these snippets are picked from, you shouldn't have issues finding the same:


Now, since this is a hack, let us limit the scope to comment one-line in the editor window. For that, let us add a new Snippet Definition Folder and name it Comment:


Next, let's add a Comment Line Snippet Definition within the above folder:


Again, since this is a hack, in the Script field just add  a # and save the new folder and the definition:



Now, let's get back to our SPE window and see if the snippet appears in the list and put it to test:

Seems good so far. Next, put the cursor to start of a PowerShell Script code line and click the Comment Line option , press OK to see it working!



So, next time when the content editing team says "Hey, can you run that PS script to push data from the content tree to an external api?", configure the script as  a snippet and just let them know how to run the script from the snippet window and I'm sure they will be happy that they don't have to depend on you anymore!


Comments

Popular Posts