Tuesday, May 14, 2013

Using SharePoint REST services from workflow with POST method

SharePoint 2013 has a completely new workflow engine. With that engine we got some new actions and options that help us in creating our bussiness process using workflows. One of them is Call HTTP service action that enables us to leverage an external web service from our workflow.
On the other hand Sharepoint 2013 comes with a new REST interface that enables us to access and use its resources.When we combine these two new features we get powerful new ways for using our SharePoint workflows.

In this post I will show you how you can use this new action to POST new data to your SharePoint site.

I have created a SharePoint list  named WorkflowItems to use for example. We will create items in this list from a workflow using SharePoint REST services. To keep it simple I will leave it with only one column (Title).

Now we can open our SharePoint Designer and create a new site workflow and call it WorkflowItemWizard.

In our new workflow first we will create couple of dictionaries. Dictionary is a new variable type in SharePoint workflows that is used for defining collections of key/pair values and is very important for the Call HTTP service action.

To create a dictionary add Build Dictionary action to you workflow. Using that action specify two new keys Accept and Content-Type and set the values for both of them to application/json; odata=verbose like in the image below


Set the output to a dictionary with a more meaningful name, call it "header". This dictionary will be used as a header for our request.

Next create another dictionary that you will call metadata and in it add a key called type with value SP.Data.WorkflowItemsListItem.

And finally you will need one more dictionary that will contain your request data. Call it parameters, add key __metadata and set its value to the previously created dictionary metadata


Add one more key, call it Title and set its value to New item created from workflow.
Now you are ready to call the web service. Insert action Call HTTP service.
The URL you will require will look like this:
https://<site>/<web>/_api/web/lists/getbytitle('WorkflowItems')/items
and the HTTP method is POST.

Set the request to your parameters dictionary. To set the header of your request go to :
right click on the Call HTTP service -> properties -> RequestHeaders and set it to your header dictionary.


This is how the complete workflow looks like:



Now you can deploy your workflow and run it. Once it is over you should have a new item created in your list.
From this example you can continue building your solutions, you can add more fields, or use other available REST services for updating list items or deleting them.

One of the biggest advantages using this method to create or update list items is it is not limited to the site in which you workflow runs. Using REST services you can now access and create items in lists on other sites.

If you want to know more about SharePoint 2013 REST you should check this out :
Programming using the SharePoint 2013 REST service

Also check this excellent post on getting data into your workflow from SharePoint using Rest services:
Calling the SharePoint 2013 Rest API from a SharePoint Designer Workflow

34 comments:

  1. It is a really useful informatuion, I will share it with all my colleagues. Thank you for posting it. This is one of the best explanation on using Sharepoint I have ever read. Well done!
    sharepoint digital signature

    ReplyDelete
  2. How do i create item if it contains lookup column ?

    ReplyDelete
  3. Hello Pradeep,

    This is how I was able to create an item with a lookup column. I have expanded my example list with a lookup column and named it "LookupColumn" :). When you are using REST service it will use the ID of the item in the lookup column and because of that it will have a property that in my case looks like "LookupColumnId" (notice the Id part). This property is of "Int32 type". So in the dictionary where you are setting your item properties you should add another key that looks like "YourColumnNameId", set its type to Int32, and the value to match an item id in the lookup list. Hope I was clear.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Good day. Can anybody ask me to - how do that method to create list item in other site level.
    Example, Site 1: http://site1, Site 2: http://site1/sites/site2. How to create copy of list item from site 1 to site 2 list?
    I write correct api address and SPList name - but Item don't create. Why so and how to correct it?

    ReplyDelete
    Replies
    1. Hi,
      Borislav thanks for the post!
      Ceprem,
      Have you ever found out how to make it work.
      The workflow runs with no errors, but no item gets created in the other list.
      Thanks,
      Laura

      Delete
    2. I Have the exact same issue. Workflow runs without errors, but nothing is created in the target list (I'm on SharePoint Online, Office 365). Any updates on how to get it to work in SharePoint Online is appreciated!

      Delete
    3. Same issue. no new list item. What's gone wrong?

      Delete
  6. Hello thanks for the great information. Now that I got the workflow to "create new items" I am wondering what it would take for the workflow/web service to "update existing" items.

    ReplyDelete
  7. Hi, my workflow it's attached with SharePoint Online 2013 document library. My library it's configured with document management options: versions, checkout-in, etc. Then i to configure in my SPD 2013 WorkFlow a REST service call with the apropiate action, similar to this: https://domain/sites/sitecollection/subsite/_api/web/GetFileByServerRelativeUrl('/sites/sitecollection/subsite/mylibary/[%Current Item:Nombre%].[%Current Item:Tipo de archivo%]')/CheckIn(comment='Comment',checkintype=1) The MSDN magazine say that calling this service we can approve the document with a major version. But this call response with UNAUTHORIZED, please help. http://msdn.microsoft.com/en-us/magazine/dn198245.aspx I have my SPD 2013 Request Header dictionary configured.

    ReplyDelete
  8. Thanks very much for this great article;this is the stuff that keeps me going through out these day. Kelly Services Login

    ReplyDelete
  9. I'm trying to create a list item in different site collection, getting Unauthorized Exception, I'm the site collection admin on both sites, using SP online.

    ReplyDelete
    Replies
    1. Did you get a solution to this? I am running into the same problem.

      Delete
    2. try allowing workflows to run with app permissions (site feature), then add the id of the workflow with permissions. i.e. how its done here:

      https://rogereriksen.wordpress.com/2013/05/24/create-a-sharepoint-site-using-rest-in-workflow-with-sharepoint-designer/

      Delete
  10. We have been trying to create a Project Server workflow with SPD2013 that creates new list items on the project site, but like Сергей Оленин and sekhar reddy we encounter 'unauthorized' if we use the App Step and without the App Step the workflow seems to run, but no new item is created. Can you help with this? Thanks!

    ReplyDelete
    Replies
    1. Hello, I'm curious if you succeeded with inserting rows into another site? If you did, would you mind sharing how you did it? Thanks much

      Delete
    2. In your request header dictionary, add a field called Authorization with no value (leave it blank). Do not forget to insert your call http service into an App Step.
      Good luck.

      Delete
  11. Hello,
    I am getting Bad Request error while posting to a share point online list from the designer workflow

    ReplyDelete
  12. Thanks to you! Really helped understand the Call HTTP Web Service Action. Good clarity in the article.

    ReplyDelete
  13. Thanks to you! Really helped understand the Call HTTP Web Service Action. Good clarity in the article.

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. This is a very helpful and informative post! Could you please tell me what I would need to do to modify your example to insert a row into a list on another site collection? Both site collections are Office 365 / SharePoint 2013 cloud sites. Thanks much for any help you can provide!!

    ReplyDelete
  16. Hi.

    I'm trying to create an item in another list found on another site.

    1, In your fourth image, it shows that you have used the variable requestData as your request content. Where is it created/used previously?

    2. Is it possible to create a list item in another list found on another site?

    ReplyDelete
  17. You explained very well manner and easy to understand ..Keep it up
    SharePoint Training Online

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Very nice. This article helped me a lot, but just like many others I was looking to add items on other sites, and I had just the same issues as described earliner. Problem was solved by using elevated permissions. Link to solution: https://msdn.microsoft.com/en-us/library/office/jj822159.aspx

    ReplyDelete
  20. Great Article, any hints on how to use this to create a folder in a library in another site?

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. wow!! No Comments !just want to say its really very helpful and information for share point user.

    SharePoint Tutorial

    Thanks

    ReplyDelete
  23. How does the requestData should looks like? Is it dictionary with the coulmn name and value?

    ReplyDelete
  24. Very good article on setting up!! I hit one snag though, anyone know what this error may be:

    {"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"Found a node of type 'PrimitiveValue' when starting to read the property value; however, a node of type 'StartObject' was expected. The '__metadata' property must have an object value."}}}

    I have deleted and recreated the workflow multiple times and double checked my entries and this error keeps coming up. Thanks so much again for the walkthrough!

    ReplyDelete
  25. Thanks for the article, i'm trying to insert row in a different list based on status change in current list. But I'm receiving "Bad-request" as response. What am i doing wrong here. Anyone who tried this,can share it here?

    ReplyDelete