POST to WebAPI from a SharePoint Workflow
There seems to be a good amount of information on consuming RESTful services in SharePoint workflow, but there are limited articles on POST. In this article I will show you how to POST a complex object to WebAPI. I say “complex” but it is really just a data class, WebAPI only wants one parameter on POST so that parameter better be good! I am just going to jump into the steps and will explain after so you can get going.
Step 1: Have a WebAPI. If you do not I have one you can use the zipped up at the bottom of this article.
Step 2: Create your workflow
Step 3: Create “Header” dictionary with the below fields (make sure Authorization is blank!)
Item |
Type |
Value |
Comments |
Accept |
String |
application/json;odata=verbose |
|
content-type |
String |
application/json;odata=verbose |
|
Authorization |
String |
|
This has to be set to empty string to force auth. |
Step 4: Create “Data” Dictionary
Item |
Type |
Value |
Comments |
Action |
String |
Whatever you want to log |
|
ID |
String |
Current Item:ID |
|
User |
String |
Current Item:Modified By |
Currently using email address. Domain user will not encode in JSON correctly. |
Step 4: Create HTTP Web Service Call and use the following settings:
Address: https://YOURSERVER/api/Demo
RequestHeaders: Header
RequestContent: Data
RequestType: HTTP POST
Step 5: Done! Data will post to your WebAPI and you can save it to SQL or send it to your grandmother via email!
OK, that was easy, but let’s dig in a little. Here are some thoughts/notes on this whole process:
Most issues with the POST are authorization related. Setting the header “Authorization” to empty string forces the system to perform authentication or it will try anonymous (which will fail miserably).
Workflow will handle a moderately complex object, it does not like arrays. This is about as complex as it should get: {"Action":"Some Action", "ID":"10", "User":"Dan Biscup"}
Under the covers it is encoding as JSON, claims user names have \\ in there and it will break.
WebAPI likes 1 parameter in the POST, so use a complex object, I usually keep these in a separate dll/project that we can use in other projects.
I like to keep a "GetAll" method that just returns the controller name so I can make sure it is running in IIS like /api/demo.
Comments
- Anonymous
October 13, 2015
Thank you http://www.idea-novin.ir/