Following on from my last post of using GET REST API in workflows, this post will show how you can create an item in a list using the POST REST API.
Creating a list Item using REST API.
Using the same list as before, on my SharePoint site, I have a list. The list is nothing special, it’s just an example of some data.
Person: User field
Colour: Choice field
You wouldn’t do this in real world scenario, but my demo will demonstrate when you run the workflow on any item, it will just create a new item in the list.
A better real world scenario might be when an item is added to this list, some of the information needs to be copied and created in another list, or when an item is changed a workflow fires to copy the information to another list similar to a history list.
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.
Click on Variable:dictionary, and create a new variable called Headers.
We need to create another Dictionary now, this is to store the “type”. When you create an item with REST API, you need to provide the __metadata with the ListItemEntityType. Typically this item is SP.Data.[ListName]Item. However, to be absolutely sure, you can use this REST API call in the browser to find out.
One more dictionary needs to be created, this is the RequestContent for the POST Call.
|PersonId||String||Current Item:Created By (return field as: User Id Number)|
When you create this, ensure that __metadata has 2 underscores in front of it.
All fields are the internal names.
User fields all have an Id field with it, in my case the Person field has a PersonId field, it is this that I set with another User Id.
Choice fields just accept a valid string.
Taxonomy fields are a little different, see section below.
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
Set the HTTP method to HTTP POST.
After entering the URL and setting the Method, click the dropdown at the far right on the line in SPD and select properties. Set the RequestHeaders to the Dictionary Variable:Headers you made in the first step. Set the RequestContent to the dictionary Variable:NewItemMetadata. 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 Created 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 Created.
- I log success to the history list.
In the else statement of if “responseCode equals Created” I grab the error message from the ResponseContent dictionary and output the value to an error message. Then I display the error message in the history list. To grab the error the dictionary path is error/message/value
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, after a moment a new entry is added to the list.
Updating Taxonomy/managed metadata column in POST REST API calls.
To be able to update a Taxonomy column in a list you would need to create the following 2 dictionary’s first, then assign 2nd dictionary to the column.
<Label Value> – the actual value of the taxonomy term
<TermGuid> – The guid of the taxonomy term found in the termstore.
The WssId can always be -1 as SharePoint can work that out itself, but it requires a value.
It is the final dictionary above that you would set in the web service call as the RequestContent.