Skip to main content

Start a SharePoint Designer 2013 workflow​ from another workflow using REST api

In SharePoint 2013, Microsoft introduced the new Workflow manager for managing the SharePoint 2013 workflow which needs to be installed separately. At the same time, the workflow service (2010 workflow) was retained as same. So by doing this, we have the option to create both SharePoint 2013 and SharePoint 2010 workflows using SharePoint Designer 2013.

Many new features were introduced in 2013 workflows such as stages and REST calls within workflow which definitely makes workflow a powerful tool for a developer.

Another interesting feature is the ability to trigger a 2010 workflow from the 2013 workflow. This will give the developer capability to club both the workflows together in order to achieve complex functionalities.

But there is one drawback, we don't have an out of box option in SharePoint 2013 workflow to start another 2013 workflow. Only a SharePoint 2010 workflow can be started.
But we do have an interesting alternative approach to achieve this using REST API call from a SharePoint 2013 workflow.

In this post, we will discuss in detail on the steps to achieve this.

In this example, we will see how we can trigger a list item SharePoint 2013 workflow from a SharePoint 2013 site workflow.


Step 1: Create Site workflow

Create a list and create a SP 2013 workflow using Designer. 
Name the workflow as "List2013WF". Lets use this workflow to trigger a mail. 
Set the workflow on item change. Now create some items in the list.



The next step is to create a site workflow. Name it as "Trigger 2013 Workflow" . 




Now we have the initial set up ready for testing out the scenario. We will see how we can start the List item workflow for a particular item (eg: ID = 5) from the site workflow.

Step 2: Get the GUID of the list

To trigger the workflow of an item, we need the following 
1. Subscription id of the workflow
2. Item ID ( SharePoint ID of the item)

 There can be multiple workflows attached to the same list, we will see how we can get subscription Id for the workflow.

To get the subscription details of the workflows, following REST query can be used.   

/_api/SP.WorkflowServices.WorkflowSubscriptionService.Current/EnumerateSubscriptionsByList(guid'{List GUID}')?select=Id,Name

To execute this REST Call, we require the GUID of the list. We can get that using workflow itself, but we will avoid that in this post to keep things less complicated.[I will explain that in a different post]

Lets use the classic approach of taking the ID from List Settings. Navigate to List ⇛  List Settings ⇛  get the URL from the browser ⇛ decode the url to get the GUID of the list.

The url will be like this ../_layouts/15/listedit.aspx?List=%XXXXXXXXXX%XXXXXXX%XXXXXX%XXXXXX%XXXXXXXXXXX%7D

use an online decoder to get the list id 

/_layouts/15/listedit.aspx?List={XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}

so we have the list GUID : XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

Step 3: Get all the workflows attached to the list

Now we have the list GUID. Lets get all the workflows attached to the list.

Lets create a dictionary which will be used as Request Header for the REST call.





Content-Type
application/json;odata=verbose

Accept
application/json;odata=verbose

We will call the above rest service using "Call HTTP Web service" action from workflow actions.

URL: https://yoursite/_api/SP.WorkflowServices.WorkflowSubscriptionService.Current/EnumerateSubscriptionsByList(guid'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')?select=Id,Name

Set the method as HTTP Post

Create a dictionary to get the response content : JSONResult




















From the above REST Call, we will get the Name and Subscription Id of the workflow subscription. By putting an If condition, we can get the Subscription Id of the workflow by checking it with the name.

To do that lets take the d/results by Get from dictionary action and save the values to a dataset (dictionary).

Create an Integer variable and using Count Items in dictionary action and get the count of items in the dataset.

There can be multiple workflows attached to same list, so we need to get the subscription Id of the workflow that we want to start.

Lets loop through the workflows in the list and get the subscription Id of workflow by comparing each item based on Workflow Name. We are adding a loop with condition and iterating through each item in JSONResults (Response content from the REST Call). (setting index as 0 and looping till it matches the count. 

For each item, we are checking the Workflow Name ("List2013Workflow") and finally we will get the subscription id in variable 'ListWorkflowGUID'. 









Step 4: StartWorkflowOnListItemBySubscriptionId


Now we are into the final step of starting the list workflow. To start a list workflow for any item, we can pass the item id and workflow subscription id to the below REST api call.

URL /_api/SP.WorkflowServices.WorkflowInstanceService.Current/StartWorkflowOnListItemBySubscriptionId(subscriptionId='{Workflow Subscription ID}',itemId={ID of List item})



HTTP Method : POST 














This will start the List workflow for the particular Id (SharePoint Id : 5). Even if the list has multiple workflows, it will start only the workflow called using subscription id.


Complete Workflow Screenshot for reference. 





Hope this will be helpful. Post your queries in comments.. 😊


Comments

  1. i get bad request mysite/_api/SP.WorkflowServices.WorkflowSubscriptionService.Current/EnumerateSubscriptionsByList(guid'{D8C8748D-8591-43E7-AFAD-6A207DA02CE4}‘)?select=Id,Name

    ReplyDelete
    Replies
    1. Are you doing cross-site query ?

      Delete
  2. Hi. I also get bed request; when I check SharePoint logs it seems the request is performed at root site and not in the sub site...

    ReplyDelete
  3. I get a bad request the first time my workflow runs (the one that calls the workflow from web service). If I run the primary workflow again it will trigger the other one with no issues. This makes no sense.

    ReplyDelete

Post a Comment