Requirement: Call REST API in SharePoint designer workflow.
Using REST API in SharePoint Designer Workflow
SharePoint Designer workflow actions are very limited, and we can utilize REST API calls to extend them. Here is how to call a REST API method from SharePoint Designer workflow action: Call HTTP Web Service. In this article, let’s call a REST API method from the SharePoint Designer workflow to get the number of items from a SharePoint Online list.
Step 1: Create a New SharePoint 2013 Workflow
- Create a new list workflow in SharePoint Designer. Make sure its Platform Type is set to “SharePoint 2013” in SharePoint Designer.
Create Necessary Workflow Variables:
Once the workflow is created, we need to create the necessary variables.
- Click on the Local Variables button on the Ribbon >> Click on Add button >> Provide a name to the variable and set its type. Here is the list of variables I’ve created.
Name Type requestHeader Dictionary responseContent Dictionary listItems Dictionary responseCode String count Integer
Here is my workflow variables created:
Step 2: Build a Dictionary Object for Request Header
To start with, We need to set the value of requestHeader variable. We need this header dictionary object to get the response in JSON format as SharePoint REST APIs return XML.
- Place the cursor under Stage 1, Click on Action >> Build Dictionary
- Set the Output to Variable:requestHeader first and then Click on this link in the action >> Click on Add button >> Set the Name, Type and Value Parameters as below:
Name Type Value accept String application/json;odata=verbose content-type String application/json;odata=verbose
- Now, the requestHeader dictionary object should look like:
Step 3: Call HTTP Web Service
Next, we need to add Call HTTP Web Service action to call the REST method.
- Click on Action >> Choose Call HTTP Web Service action
- Once the action is inserted, Select the Call HTTP Web Service action, Click on “Advanced” button from the toolbar.
- Set the following parameters in the Advanced dialog box from the variables we’ve created.
- Address: This is the REST API End-Point. We need to construct it according to our requirements. Click on the little three dots button to open String builder dialog. Click on Add or change lookup. Next dialog will be opened is Lookup for String. Select Data Source as Workflow Context and Field from source as Current site URL. E.g. Here I’ve constructed it as: “[%Workflow Context:Current Site URL%]_api/web/lists/getbytitle(‘[%Workflow Context:List Name%]’)/items” to retrieve all items from the current list.
- RequestType: As we are retrieving data, this should be set as: HTTP GET
- RequestHeaders: This is the headers of the REST call we will assign dictionary value that we created in the first step (requestHeader)
- ResponseContent: This is a Dictionary variable we receive from the result from the REST call.
- ResponseStatusCode: This is the status of REST call. We may need it to check the status of the call.
Step 4: Process the Response Received
Once we get the response from the REST API call, we need to traverse to d\results node to get list items data, as the API call returns result in below format (Thanks Fiddler!):
- Insert Get an Item from a Dictionary action to extract results from JSON response. We need to traverse to d/results node of the JSON responseContent.
- Next, we need to Count Items in a Dictionary action to get the number of list items.
- Then we use “Log to History List action to record the output we received.
Here is how the workflow looks like in SharePoint Designer
- Set Workflow Start Options: Click on “Workflow Settings” in the toolbar and set which action should trigger this workflow from “Start Options” section. E.g. Start workflow automatically when an item is created.
- Save and Publish the workflow.
Here is the workflow in action:
Here is the Microsoft documentation of complete REST API methods: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-rest-endpoints