I’ll get straight to the point. cURL is a command line tool for transferring data with URL syntax, supporting HTTP, HTTPS, etc. oData is a Web protocol for querying and updating data built on top of upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON.
Microsoft SharePoint 2010 exposes their Lists using the oData protocol which you can access from the following URL – http://<sharepoint>/_vti_bin/ListData.svc.
Here’s how you can do all 4 CRUD (Create, Read, Update, Delete) operations on your Microsoft SharePoint 2010 Lists using cURL which you can easily translate it to JSON and JQuery once you understand what’s going on.
Setting up
Basic syntax (for our purpose):
[sourcecode language="bash" autolinks="false"]
curl -ntlm -netrc http://sharepoint/_vti_bin/[moreurl]
[/sourcecode]
where:
[sourcecode language="bash" autolinks="false"]
.netrc (mode 600)
machine sharepoint login user password pwd
[/sourcecode]
Create
In order to create an entry into the list, you have to pass in the data as shown below with the command “-d”. I’m passing in JSON in this case as set with the command “-H”, but you can pass in an XML string if you wish too. I find JSON shorter and more concise. You use the POST method request in order to create an entry or item.
[sourcecode language="bash" autolinks="false"]
curl –ntlm –netrc
-d "{ Title: ‘January Team Meeting’, StartTime: ’2011-01-15T17:00:00′, EndTime: ’2011-01-15T18:00:00′ }"
-H content-type:application/json
-X POST http://sharepoint/_vti_bin/ListData.svc/Calendar
[/sourcecode]
Notice I did not set all the fields from the Calendar List. You do need to set all the fields that are required, but those not required and not specified will be set to NULL or the default value.
You will get back a reply with the oData entry if successful.
Read
Getting data is as simple as doing a normal GET method request from the List itself.
[sourcecode language="bash" autolinks="false"]
curl –ntlm –netrc http://sharepoint/_vti_bin/ListData.svc/Calendar
[/sourcecode]
Here is a quick list of what kind of filters you can do.
Syntax:
http://sharepoint/_vti_bin/ListData.svc/{Entity}[({identifier})]/[{Property}]
Example to get budget hours for Project $4:
http://sharepoint/_vti_bin/ListData.svc/Projects(4)/BudgetHours
Example to get Projects for Clients in Chicago:
http://sharepoint/_vti_bin/ListData.svc//Projects?$filter=Client/City eq ‘Chicago’
Example to get a Project and its related Client:
http://sharepoint/_vti_bin/ListData.svc/Projects?$expand=Client
Shows me parent that is associated with this XML
QueryString parameters for REST:
$filter
$expand
$orderby
$skip
$top
$metadata (will bring back all the XML metadata about the object. Think of it like WSDL for your REST call)
You can stack these parameters
Update
There are 2 ways of updating Microsoft SharePoint 2010 oData items. Notice the URL is pointing to the specific entry, in this case the entry with id=2 in the Calendar list.
One way is to use the PUT method request which essentially replaces an entirely new version of data into that entry. This is similar to creating an entry or item – You do need to set all the fields that are required, but those not required and not specified will be set to NULL or the default value.
[sourcecode language="bash" autolinks="false"]
curl –ntlm –netrc
-d "{ Title: ‘January 2011 Team Meeting’, StartTime: ’2011-01-15T17:00:00′, EndTime: ’2011-01-15T18:00:00′}"
-H content-type:application/json
-H If-Match:*
-X PUT "http://sharepoint/_vti_bin/ListData.svc/Calendar(2)"
[/sourcecode]
Notice that you need to have the header “If-Match:” in order to enable the update to happen. This is to prevent concurrent updates and modifications to happen. Essentially, the right way is to read the data and get the “If-Match:” header tag which looks something like this If-Match: W/”X’000000000000D2F3′”, then put it in the header when you update. However, in this case, If-Match:* essentially ignores all concurrency problems and just tells the oData service to update.
The other way is to modify only the changes made, keeping all the other fields with data intact. You use the MERGE method request instead as seen below.
[sourcecode language="bash" autolinks="false"]
curl –ntlm –netrc
-d "{ Title: ‘January 2011 Team Meeting’}"
-H content-type:application/json
-H If-Match:*
-X MERGE "http://sharepoint/_vti_bin/ListData.svc/Calendar(2)"
[/sourcecode]
It will return 200 (OK), 201 (Created) or 202 (Accepted) if successful. (I can’t remember which at the moment)
Delete
Deleting is as simple as using the DELETE method request.
[sourcecode language="bash" autolinks="false"]
curl –ntlm –netrc
-X DELETE "http://sharepoint/_vti_bin/ListData.svc/Calendar(2)"
[/sourcecode]
A successful delete will return a 200 (OK) status code.
Summary
So that’s how you do CRUD operations with all that low level HTTP method request. Create – POST, Read – GET, Update – PUT or MERGE, Delete – DELETE. Remember, in order to update, you need the “If-Match:” header when you send a PUT or MERGE method request else it will fail.
[...] the topic of the SharePoint REST services, Justin Lee has a great post that gives a quick overview of the core functionality. » Share About Charles ChenI'm a [...]
[...] CRUD operations on Microsoft SharePoint 2010 oData Lists with cURL [...]
[...] CRUD operations on Microsoft SharePoint 2010 oData Lists with cURL [...]
Justin,
I’m wondering if you’ve experimented with updating multi-choice fields using the REST interface. What I’ve found is that the behavior seems to be “additive” such that if I have a multi-choice field that has no value, and I send an equivalent value of ["a", "b"], it will set the value to ["a", "b"]. But if I then try to send a new value ["c"], it will set the value to ["a","b","c"] instead of the expected ["c"]. Likewise, sending [] does not reset the value. Sending ["a","b","c"] will result in a weird value of ["a","b","a","b","c"] but this resets to ["a","b","c"] if you save the value from the UI.
An example is if I have a field called “Skills” with valid choices of “WCF”, “WPF”, and “WF”. I can set the field to ["WPF","WF"] by POSTing:
{“Skills”:[ {"__metadata": {"uri": "CandidateSkills('WPF')"} }, {"__metadata": {"uri": "CandidateSkills('WF')"} } ] }
But I cannot find a way to “reset” this field once I set it — it becomes fully additive (any values specified are just appended to the list oif values.).If you’ve worked with multi-choice fields, your input here would be greatly appreciated.Thanks!
I am experiencing this same “additive” problem. Did you ever find a solution, Charles? Do you have to change some setting in the headers for JSON request? Thanks.
Michael: I never found a solution for this and ended up writing a custom web service endpoint instead.
[...] If you want to dive into http verbs in the listdata.svc, install cURL and follow the awesome tutorial at JustinLee.sg. [...]
Can cUrl be used to upload document into an SP2010 document library? …if so, how?