Sitecore Powershell – From powershell to .NET Code

Sitecore powershell is a must have module on all Sitecore instances. The Sitecore PowerShell Extensions (SPE) module is a Sitecore development accelerator which can drastically increase your productivity and curtail the amount of time it takes to deliver a Sitecore solution.

I wanted to get start with the Sitecore powershell and decided to implement a script which will ask the user some input which I will process in the backend, that is, in the code. So, let’s get start.

First of all, let’s define the scenario. The user should be able to

  1. Insert a text in a textbox.
  2. Select a template from a droptree.
  3. Pass the input information to a method in the code.
  4. Return and alert the result.

The Powershell Script

The powershell script will look as follows:

This feature is called splatting and it allows for a better understanding of the script:

splatting.PNG

What is does now is that it will create a Sheer UI modal with a textbox and a droptree as shown below:

capture

The statements shown below are responsible to call the method from the code. The New-Object will create a new instance of the class Demo and the GetResponse() is a method of the Demo class.

Capture.PNG

Note that you can also call a static class as follows:

[Namespace.Demo]::GetResponse($userInput, $templateItem)

Once the method is called, you can perform the different logic on the value then returns it back to the powershell. As example:

The $response will contain the result from the method GetResponse() which you can then alert or perform more processing via the powershell script.

Special thanks to Michael West for introducing me to the Powershell world 🙂

References

  1. Sitecore Powershell GitBook
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s