SharePoint Designer 2013 Workflows and GET REST API


SharePoint Workflows have been around for a long time, and with Microsoft flow now firmly taking hold the need for SharePoint workflows might be less. However, I recently had to work with SharePoint Workflows and learnt a few things around using REST API and I wanted to share my knowledge.

Reading a list/Item using REST API.

On my SharePoint site, I have a list. The list is nothing special, it’s just an example of some data.

Title: String
Person: User field
Colour: Choice field

All I’m going to do in the workflow, is when it is run, it will grab the items in the list, and then display the results in the history list. The point of this demo is to show how to make REST API calls and grab/loop through data.

Creating the workflow

In SharePoint Designer, I have gone to my list, and created a new workflow.

  • First, we need to add a Dictionary Item. This is so we can create a header for our HTTP Call. Type in “Dictionary
    and then in the options to choose from select Build Dictionary.

    Click on “this”.

    A dialog will appear, and it is here we need to add two headers.
Name Type Value
accept String application/json;odata=verbose
content-type String application/json;odata=verbose

Click on Variable:dictionary, and create a new variable called Headers.

Because SPD is a pain, it has already created a variable called dictionary, if you click on Local Variables from the ribbon, you can delete this variable. This will ensure you keep only the variables you are using within your workflow and save confusion later. Always give your variables sensible names, just as if you were writing code.

  • Now we can call a HTTP web service. Underneath the dictionary, start typing “http” and press enter to insert Call HTTP Web Service.
    Click on “this” to open up the dialog box, and then click on the … for the string builder dialog box.

    I like to use as much dynamic values as possible so my URL in here looks like the following:

    [%Workflow Context:Current Site URL%]/_api/web/lists/getbytitle('[%Workflow Context:List Name%]')/items?$select=Title,Colour,Person/Title,Person/Name,Person/EMail&$expand=Person
    

    In the above API call I’m bringing back additional information about the Person from the person column. See at the end of this post about bringing back other information on a person column.

    After entering the URL, click the dropdown at the far right on the line in SPD and select properties. Set the RequestHeaders to the Dictionary you made in the first step. Set the ResponseContent to a new variable, and the ResponseStatusCode to a new variable. Click OK.

    The ResponseContent will be populated with the results, and the ResponseStatusCode will be populated with OK if successful, or a different value if something went wrong.

  • After the Web service call, I add an If Statement, to test that the variable responseCode equals to OK.
  • Now we want to grab the results. So now we will grab a dictionary item. Type “Get” and press enter to insert Get an Item from a Dictionary.
    Click “item by name or path” and type “d/results”
    Click “dictionary” and select “ResponseContent”
    Click “item” and create a new variable. This variable is a dictionary too that will contain the individual items. I’ve called mine DemoList.
  • We need to count the number of items in the new dictionary variable. I do this to ensure that we have returned items.
    Type “Count” and press enter to insert count items in a dictionary.

    Click “dictionary” and select variable “DemoList”.
    Can leave output Variable to count.

  • Put another If statement, and check that Count is greater than 0.
  • We now need to create a variable that will be our indexloop.
  • Add a loop with a condition. (Alternatively, we could have skipped the last step and added a “loop n times” instead).
    Set the condition to read. “The contents of this loop will run repeatedly while: Variable: indexloop is less than Variable: count

  • Now I’m going to get the values of Title, Colour, Person/Title and Person/Name from the DemoList dictionary.
    Type “Get” and press enter to insert Get an Item from a Dictionary
    Click “item by name or path” and type “([%Variable: indexloop%)/Title”

    Click “dictionary” and select “DemoList”
    Click “item” and create a new variable.

    Repeat this for all the parameters.

  • Still inside the loop underneath gathering the parameters we need to raise the index variable by one.
    Type “Calc” then press enter to insert Do Calculations
    Click first “value” and select the Variable: “indexloop”

    Click second “value” and set to 1.

    Set the output to a number variable. I called mine NewIndex

  • Now you need to set the variable NewIndex to indexloop.
  • Lastly, for demo purpose, I’m logging the results I’ve got to the history list. This logging to history lists is still within the loop.
    Type “Log” then press Enter to insert log to History List.
    Then I’m just logging out the variables I’ve captured.
  • Don’t forget to Transition to Stage “End of Workflow” at the end.

Publish your workflow.

When I run the workflow against any list item I see the following results in my History List.

Bringing back additional Person Data in REST API calls.

To bring back a person column data (or a lookup column), you need to expand it in a REST API call, you also need to include the values you want to bring back from the expanded column. A list of all possible values you can bring back for a user column can be found if you type the following in a browser (after already signing into SharePoint that is).

https://<tenant>/sites/<site>/_api/web/lists/getbytitle('<ListTitle>')/items(1)/<UserFieldInternalNameColumn>

*I’m using XV – XML Viewer chrome extension for the layout.

Advertisements