SharePoint 2013: Introduction to Workflow Services API
Introduction
Workflow Services API are one of the newest feature extensions that are shipped with SharePoint 2013. These API allows you to perform a lot of operations on Workflows couple of them is shown in the list as below.
- Create Workflows
- Edit Workflows
- Start Workflows
- Stop Workflows
- List Workflows
- Copy Workflows from one site to another
- Associate Workflows to lists
- Export Workflows to WSP …. And the list goes on
Since this API is a part of SharePoint CSOM, it is accessible using both JavaScript & C# Managed Code.
Workflow Services are primarily meant to work with SharePoint 2013 Workflows (or WF4 Workflows) but also shipped with limited support for SharePoint 2010 Workflows (or WF3 Workflows).
Workflow Services API
The Workflow Services API consists of four Services to deal with different scenarios:
Workflow Instance Service
- Can be used to Query Workflow Instances
- Can be used to Create new Instances based on existing Associations
- Can be used to Create new Instances of List Items and Sites
- Can be used to Communicate existing Instances
- To Cancel Workflow
- To Terminate Workflow
- Publish Custom Events
Workflow Subscription Service
- Can be used to Query Workflow Associations
- Can be used to Create Workflow Associations
Workflow Deployment Service
- Can be used to Query Workflow Definitions
- Can be used to Validate Workflow Definitions
- Can be used to Create Workflow Definitions
- Can be used to Validate Workflow Definitions
Workflow Interop Service
- Can be used to Interact with SharePoint 2010 Workflows in SharePoint 2013
In this first article on Workflow Service we will consider a very simple scenario where we will list down all the workflows created [only WF4 Workflows] with in a site, to see important details like Name, ID and Published Status.
Demo
In order to step up this demo we have one site “Development” which is created as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/1.png?w=450&h=345
If we open this site in SharePoint Designer, and visit the Workflow Section on the left as shown below, we found no workflows have been created as of now.
https://howtodowithsharepoint.files.wordpress.com/2015/06/2.png?w=450&h=182
So, first of all, let’s create a new Reusable Workflow, give it a name “Parent – Workflow”. Make sure Platform Type must be “SharePoint 2013 Workflows”.
Click OK to save the Workflow definition.
https://howtodowithsharepoint.files.wordpress.com/2015/06/3.png?w=450&h=354
Now add a simple step called “Logging” to this workflow and add a “Log to History” activity to this list.
Check for the errors
Save the workflow
Publish the workflow as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/4.png?w=450&h=320
Once you publish the workflow it will be placed in the right container in SharePoint Designer.
https://howtodowithsharepoint.files.wordpress.com/2015/06/5.png?w=450&h=132
Now go to any list and add this workflow to the list
https://howtodowithsharepoint.files.wordpress.com/2015/06/6.png?w=450&h=119
https://howtodowithsharepoint.files.wordpress.com/2015/06/7.png?w=450&h=177
In the similar fashion create another workflow and let say called it as “Unpublished Workflow”, the only difference this time is we are not going to publish it.
Just create it and save it, that’s all.
https://howtodowithsharepoint.files.wordpress.com/2015/06/8.png?w=450&h=63
OK, this will suffice to prepare our environment for this demo.
Now the next task in line is to create a UI element that can trigger the action to list workflows. For this we have created a simple page with a Button on it “List Workflows”
https://howtodowithsharepoint.files.wordpress.com/2015/06/9.png?w=450&h=159
The simple HTML that brings this button up is as follows
https://howtodowithsharepoint.files.wordpress.com/2015/06/10.png?w=450&h=35
This button is hooked up with an Event Handler named “ListWorkflow()”
In order to implement the logic behind this Event Handler, we need to include the necessary references in our code:
- jQuery.js
- MicrosoftAjax.js
- SP.Runtime.js
- SP.js
- SP.Core.js
- SP.WorkflowServices.js
https://howtodowithsharepoint.files.wordpress.com/2015/06/11.png?w=450&h=89
Then proceed with the code as follows:
https://howtodowithsharepoint.files.wordpress.com/2015/06/12.png?w=450&h=56
- Initialize Context Variable
- Initialize the Service Manager object in the context of the current site
- Initialize Workflow Deployment Service Instance
https://howtodowithsharepoint.files.wordpress.com/2015/06/13.png?w=450&h=216
- Get Handle on Workflow Definitions Collection
- Get Current Workflow Definition
- Retrieve Workflow Properties from Workflow Definition
You can view all the properties exposed by Workflow Definition by placing a breakpoint on line 5 shown above:
https://howtodowithsharepoint.files.wordpress.com/2015/06/14.png?w=450&h=200
For this demo, we have taken only four properties to display:
- Workflow ID
- Workflow Display Name
- Workflow Description
- Workflow Published Status
So if we click “List Workflow” button we can be able to see the selected details for the Workflow Definitions which are stored on a specific site as show below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/15.png?w=450&h=152
- Click Button
- Site URL for which the Workflow Definitions are listed
- Workflow definition details
The information shown above is really useful to work with any Workflow Definition and we will be going to see this information in action in the series of upcoming articles on Workflow Services dealing with a bit more advanced scenario.
Hope you find it helpful.