This is the third part of a four-part series on automating workflow execution within Citrix Workflow Studio. In the first two parts of the series, I discussed how you can create Powershell scripts to execute workflows. Part 1 provided a high-level overview of creating Powershell scripts for automating workflow execution and Part 2 expanded on those scripts to discuss how to pass parameters into the workflow. In this blog, I will discuss how you can automate workflow execution with a custom Windows application. The sample .NET application I built is shown below. I am providing both the executable and full source code to allow you to expand upon this solution to meet the needs of your environment.
When would I need to use a custom application for executing workflows?
A lot of you might be asking the question – “If we can use the Workflow Studio Console to execute workflows, why would I need a custom application to do that for me?”. The answer really comes down to how you need to execute certain workflows in your environment. If you need to execute workflows “on-demand” or “on a schedule“, the Workflow Studio Console would meet those needs. Just open the Workflow Studio Console and schedule a job to run immediately or on a pre-determined schedule. However, if you need “event-driven” automation where a third-party application kicks off the execution of a workflow based on an event happening, that is where you need to be able to hook into Workflow Studio with a custom solution to perform that type of automation.
The custom application provided in the Downloads section below provides a sample for how you can tie any .NET windows application to Workflow Studio for executing workflows. Feel free to try out the sample application on your Workflow Studio machine. The full source code is also provided to allow you to customize and extend the application further based on what you need to do.
Preparing your Workflow Studio environment to use the custom application:
The custom application needs to run on a machine that contains Workflow Studio since it ties into Powershell and calls the Workflow Studio cmdlets for performing various actions on Workflow Studio. In your Workflow Studio Console, you will need to pre-deploy the workflow that you want to automate (see Part 1 for more information on pre-deploying workflows). The workflow that you want to automate can include parameters as well. Parameters for a workflow are defined in the Workflow Studio Designer under the Global Properties section (see Part 2 for more information on defining workflow parameters).
This custom application functions as any typical Windows application in that it runs under the context of the logged-on user account. The logged-on account needs to have permissions in Workflow Studio to execute workflows. These permissions are defined in the Security section of the Workflow Studio Console. For simplicity, I typically add the user accounts as a “Workflow-Admin“, but you can play with the security roles here to determine what roles are actually needed.
Using the custom application:
To use the custom application, log onto your Workflow Studio machine with an account that has permissions to execute workflows within Workflow Studio. Place WFSWorkflowExecute.exe on your Workflow Studio machine. When you launch the application, the initial view is shown below.
Click the Get Runtimes button to get the Workflow Studio runtimes that are installed on the local machine. The runtimes are listed within the combo-box below and the name of the runtime specifies which account the runtime is running under.
When a runtime is selected, all of the workflows that are deployed on that runtime are listed as well. To execute a certain workflow, you need to know which runtime you deployed the workflow on. This sample application also provides the version of the workflow as you may have deployed two different versions of the same workflow to the same runtime. Using the version numbers helps you keep them straight.
When you want to execute a workflow, you need to know if that workflow requires any parameters. The workflow parameters are defined in the Workflow Studio Designer in the Global Properties section of the workflow. As mentioned above, Part 2 of this blog series goes into detail on how to define and use parameters inside a workflow.
When specifying workflow parameters inside the custom application, the parameter name is case sensitive. (Notice how I kept the parameter name as “gMessage”). If you mistype the parameter name or use the wrong case, you will get an error message when executing the workflow stating that the parameter name is not recognized. Just keep this in mind as you type the name of the parameters into the application.
Click the Execute Workflow button to execute the workflow. If the workflow was successfully executed, you’ll get a pop-up message indicating success.
To verify the workflow was executed, you can open the Workflow Studio Console and check out the Jobs section.
Understanding the source code:
A lot of you out there will want to customize this application in your own environment. I developed this application with C# using Visual Studio 2008 and the Powershell SDK. Your development machine will also need Powershell 1.0 and Citrix Workflow Studio. For advice on setting up your Visual Studio environment, check out this article. If you follow the steps in that article, you’ll just need to add the Powershell SDK and you should be all set.
The Powershell SDK is included as part of the Windows Software Development Kit. You can get information on the Poweshell SDK here. I downloaded the full ISO and it took me quite a while to get it. When you are ready to install, you don’t need everything in the setup wizard. The Powershell SDK link provided above actually tells you what you specifically need to install to just get the Powershell SDK. To use the Powershell SDK inside your Visual Studio project, the project will need to reference System.Management.Automation.dll.
Once you get your development machine set up, you can open the solution that I provided above inside Visual Studio. Essentially all this custom application does is use the Powershell SDK APIs to execute cmdlets on Powershell. Since Workflow Studio provides Powershell cmdlets to automate various tasks, we can use the Powershell SDK to automate many Workflow Studio functions.
From my personal experience, using the Powershell SDK takes a little time getting used to. What I found to be helpful is to first write out the Powershell commands in Notepad and test them within Powershell to make sure they are functioning as expected. If you are working with Powershell variables, use the echo statement frequently to understand the current value of the variable. Then when you are ready to use the Powershell SDK in your Visual Studio project, you can convert each of your Powershell statements to the equivalent Powershell SDK code. In the source code that I provided for this custom application, I put comments into the code to explain what Powershell command I was trying to execute within that code block. Reviewing those comments should help you to get started with understanding how to use the Powershell SDK. For example, here is the ExecuteWorkflow() function that is used by the custom application. Stepping through this code inside Visual Studio and watching the variables is another great way to learn this code.
Blogs in this series:
Using Powershell to automate workflow execution within Citrix Workflow Studio
Passing parameters into a workflow within Citrix Workflow Studio
Automating workflow execution for Citrix Workflow Studio using a .NET windows application (this one)
Automating workflow execution for Citrix Workflow Studio using an ASP.NET web application