SharePoint 2013: Introduction To Workflow Services API

Workflow Services API are one of the newest feature extensions that are shipped with SharePoint 2013. These APIs allow you to perform a lot of operations on Workflow, a couple of them are shown in the list below:

  1. Create Workflows
  2. Edit Workflows
  3. Start Workflows
  4. Stop Workflows
  5. List Workflows
  6. Copy Workflows from one site to another
  7. Associate Workflows to lists
  8. Export Workflows to WSP …. And the list goes on

Being this API a part of SharePoint CSOM, it is accessible using both JavaScript & C# Managed Code.

Workflow Services are primary meant to work with SharePoint 2013 Workflows (or WF4 Workflows) but also shipped with limited support for SharePoint 2010 Workflows (or WF3 Workflows).

The Workflow Services API consists of 4 Services to deal with different scenarios:

  1. Workflow Instance Service

    1. Can be used to Query Workflow Instances
    2. Can be used to Create new Instances based on existing Associations
    3. Can be used to Create new Instances on List Items and Sites
    4. Can be used to Communicate existing Instances

      1. To Cancel Workflow
      2. To Terminate Workflow
      3. Publish Custom Events

  2. Workflow Subscription Service

    1. Can be used to Query Workflow Associations
    2. Can be used to Create Workflow Associations

  3. Workflow Deployment Service

    1. Can be used to Query Workflow Definitions
    2. Can be used to Validate Workflow Definitions
    3. Can be used to Create Workflow Definitions
    4. Can be used to Validate Workflow Definitions

  4. Workflow Interop Service

    1. 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.

In order to step up this demo I have one site “Development” which is created as shown below:

Development

If we open this site in SharePoint Designer, and visit the Workflow Section on the left as shown below, we found no workflows has been created as of now.

workflows

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.

workflows

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:

workflows

Once you publish the workflow it will be place on the right container in SharePoint Designer.

container

Now go to any list and add this workflow to the list

add

add

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.

create

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 I have created a simple page with a Button on it “List Workflows”,

list

The simple HTML that bring this button up is as follows,

html

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:

  1. JQuery.js
  2. MicrosoftAjax.js
  3. SP.Runtime.js
  4. SP.js
  5. SP.Core.js
  6. SP.WorkflowServices.js

code
Then proceed with the code as follows:

code

  1. Initialize Context Variable
  2. Initialize the Service Manager object in context of the current site
  3. Initialize Workflow Deployment Service Instance

code

  1. Get Handle on Workflow Definitions Collection
  2. Get Current Workflow Definition
  3. Retrieve Workflow Properties from Workflow Definition

You can view all the properties exposed by Workflow Definition by placing breakpoint on line 5 shown above:

properties

For this demo, I have taken only four properties to display:

  1. Workflow ID
  2. Workflow Display Name
  3. Workflow Description
  4. Workflow Published Status

So 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:

List Workflow

  1. Click Button
  2. Site URL for which the Workflow Definitions are listed
  3. Workflow definition details

The information shown above is really useful to work with any Workflow Definition and we will see this information in action in the series of upcoming articles on Workflow Services dealing with a bit more advanced scenarios.

Read more articles on SharePoint: