Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Posted Thursday, January 21, 2010 2:04 PM by CoreyRoth

What I really like about SharePoint 2010 is all of the new ways we can get at list data.  You can always use the Client Object Model, Linq to SharePoint, or the existing object model, but one neat new way to get at list data is with listdata.svc.  ListData.svc provides a way of getting information from a list (or lists using joins) using REST.  What you end up with is a nice RSS feed of list data, that you can consume with whatever client you would like.  You can construct URLs in various manners to get specific records, do joins, or perform simple queries.  I won’t go through everything that you can do with it today, but I’ll point you towards resources to do the more advanced things.

When you are getting started, the first thing you want to do is check and see if you have ListData.svc up and running.  Like any SharePoint web service, it’s located in the _vti_bin folder of any existing site, so it will work with items relative to that site.  Here is what a typical URL might look like.

http://<sharepoint-server>/_vti_bin/ListData.svc

Try hitting that URL on your SharePoint 2010 server and see if it works.  There is a good chance that you will get a 404 error.  This happened to me, so I did some searching and found Rob Garret’s post stating to go out and install ADO.NET Data Services 1.5 CTP 2.  There are a few choices, but I have seen others recommend you go with the runtime only.  I had issues installing the full package.  Once you have it installed, it still didn’t work for me, so I rebooted my server and everything worked fine when it booted back up.  My guess is you probably could just reset IIS though.

Once you have a working ListData.svc, hitting it you should get results like this. 

ListDataSvcNoParameters

You get an XML document of all lists available to be queried.  If you notice the href on each collection it gives you an idea of how you can construct subsequent URLs to get data.  In today’s example, we’re going to work with a simple task list.  We’ll look at the various ways we can get data from this list.

ListDataSvcTaskList

To get the data for this list via REST we simply just add the list name to the URL.  In my case the name of the list is called Tasks.  Here is what the URL would look like.

http://<sharepoint-server>/_vti_bin/ListData.svc/<ListName>

In my case:

http://sp2010/_vti_bin/ListData.svc/Tasks

Here is what the results look like.

ListDataSvcTasksAll

As you can see we get an RSS feed and this is how Internet Explorer renders it.  However, if we look at the actual XML of the feed, we’ll find that we get quite a bit of data back about the list.  Here is a snippet of the XML.

<feed xml:base="http://sp2010/_vti_bin/ListData.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">

  <title type="text">Tasks</title>

  <id>http://sp2010/_vti_bin/ListData.svc/Tasks</id>

  <updated>2010-01-21T19:21:27Z</updated>

  <link rel="self" title="Tasks" href="Tasks" />

  <entry m:etag="W/&quot;1&quot;">

    <id>http://sp2010/_vti_bin/ListData.svc/Tasks(1)</id>

    <title type="text">Test 1</title>

    <updated>2010-01-21T09:26:51-06:00</updated>

    <author>

      <name />

    </author>

    <link rel="edit" title="TasksItem" href="Tasks(1)" />

    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="Tasks(1)/Attachments" />

    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Predecessors" type="application/atom+xml;type=feed" title="Predecessors" href="Tasks(1)/Predecessors" />

    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Priority" type="application/atom+xml;type=entry" title="Priority" href="Tasks(1)/Priority" />

    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Status" type="application/atom+xml;type=entry" title="Status" href="Tasks(1)/Status" />

    <category term="Microsoft.SharePoint.DataService.TasksItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />

    <content type="application/xml">

      <m:properties>

        <d:ID m:type="Edm.Int32">1</d:ID>

        <d:ContentTypeID>0x01080085FF0E1548B7414787A693232497B24E</d:ContentTypeID>

        <d:ContentType>Task</d:ContentType>

        <d:Title>Test 1</d:Title>

        <d:Modified m:type="Edm.DateTime">2010-01-21T09:26:51</d:Modified>

        <d:Created m:type="Edm.DateTime">2010-01-21T09:26:51</d:Created>

        <d:CreatedByID m:type="Edm.Int32">1</d:CreatedByID>

        <d:ModifiedByID m:type="Edm.Int32">1</d:ModifiedByID>

        <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>

        <d:Version>1.0</d:Version>

        <d:Path>/Lists/Tasks</d:Path>

        <d:PriorityValue>(2) Normal</d:PriorityValue>

        <d:StatusValue>In Progress</d:StatusValue>

        <d:Complete m:type="Edm.Double" m:null="true" />

        <d:AssignedToID m:type="Edm.Int32" m:null="true" />

        <d:TaskGroupID m:type="Edm.Int32" m:null="true" />

        <d:Description>&lt;div&gt;Test 1 Task&lt;/div&gt;</d:Description>

        <d:StartDate m:type="Edm.DateTime">2010-01-21T00:00:00</d:StartDate>

        <d:DueDate m:type="Edm.DateTime" m:null="true" />

      </m:properties>

    </content>

  </entry>

As you can see in the content element, we can see the various site columns on a particular list item.  Of course, there is more we can do with REST than just view everything in a list.  If you want a specific item, you can use parenthesis and specify an indexer.  Note that it is unit-indexed, not zero-indexed.

http://<sharepoint-server>/_vti_bin/ListData.svc/<ListName>(<Index>)

In my case:

http://sp2010/_vti_bin/ListData.svc/Tasks(3)

However, when you do this, Internet Explorer will give you an error that it cannot display this feed.

ListDataSvcTaskListIndexerIeError

Not to worry though, if you view source, you still have a working XML document.  It will pretty much look like the one above minus the initial feed information.  You can take the query above and go one step further.  Say, you just want to know the status for a specific task (note that the site column is actually called StatusValue here), you can simply add it to the URL like this.

http://<sharepoint-server>/_vti_bin/ListData.svc/<ListName>(<Index>)/(<Field>)

In my case:

http://sp2010/_vti_bin/ListData.svc/Tasks(3)/StatusValue

You’ll get an even simpler XML document that looks like this.  It will create an element named after whichever site column you passed to it.

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>

<StatusValue xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">Completed</StatusValue>

The last thing I will cover is simple queries.  You can do this with $filter.  There are a number of operators you can use here, but the one I will start with today is eq (or equals).  For example, say I want to see all tasks that are completed, I would use a URL like this.  Put any literals inside quotes.

http://sp2010/_vti_bin/ListData.svc/Tasks?$filter=StatusValue eq 'Completed'

This returns results that look like this and of course the content element in the XML has the complete data on each list item returned.

ListDataSvcTaskListQuery

You can also use various other types of predicates, such as ne, gt, ge, lt, le, and, or, not, etc.  What each one does is probably pretty obvious, but if its not take a look at this MSDN reference for more information on the various filters and parameters you can use.  Skip the code stuff at the begging and scroll down a bit to find the good stuff.  This is a good start to working with REST in SharePoint, but this really is just the tip of it.  What you are learning here isn’t really just specific to SharePoint but it applies to anything you do with ADO.NET Data Services, so it might be useful elsewhere later.

One thing I will point out is that I was not able to use this with an external list.  I am guessing this is by design (which sucks), but it doesn’t look like it’s going to work.  Of course, my install could just be broken or this could be subject to change.

Comments

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Thursday, March 25, 2010 11:40 AM by Noel Fosterq

I got the base listdata.svc link to work but when I try to do any additonal parameters I get 404 error.  Any ideas?

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Friday, April 23, 2010 12:44 AM by Ashish

ListData.svc does not work with external lists in SharePoint foundation 2010 - i get a 404.

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Friday, June 18, 2010 3:23 PM by Mark

Hi Corey, great post! I followed your instructions and I can get the page with the feed but the individual list items will not show up. I assumed this was a Firefox thing and tried to use the same url in Internet Explorer 7 but I can't even get the page to load in Internet Explorer. Any suggestions?

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Friday, June 25, 2010 7:23 AM by Timen

Good walkthrough, covers it all. It looks i have the same issue as Mark. http://<sharepoint-server>/_vti_bin/ListData.svc works!!

But if I go to sp2010/.../Tasks it shows me the RSS feed Tasks without any items.

If I make a list in designer I only get the properties and still no items. Any ideas?

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Monday, August 23, 2010 11:28 AM by Steve

I cant get this to work with the "Posts" list in a blog site. All of the other lists from the blog site work fine in this manner but when i try adding the "Posts" list i get "the website cannot display the page"

Any idea why whis does not work for blog sites?

Thanks

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Monday, August 23, 2010 11:35 AM by CoreyRoth

@Steve I've had issues with it myself on particular lists, but I haven't tried it on that particular one.  I'll give it a try.

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Friday, September 17, 2010 9:05 AM by morteng

Hi

Thanks for the post!

I'm facing an issue, i have a custom ContentType with some fields that are of the type Managed Metadata, these fields doesn't get published in the listdatas.svc how to do this? I've been googling for the past 3hours with no luck :(

Thanks!

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Thursday, October 7, 2010 8:30 PM by Chris

Hi,

I face the issue trying to add to infopath gives me:

Could not open listdata.svc

Not enough storage space

please help!

# ways to access Sharepoint object model &laquo; Alen Ziver Malhasoglu&#039;s Microsoft Commerce Server Blog

Pingback from  ways to access Sharepoint object model &laquo; Alen Ziver Malhasoglu&#039;s Microsoft Commerce Server Blog

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Sunday, October 17, 2010 7:23 PM by Murali

Grate article. I am going to use XML feed in XML viewer with xslt..

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Friday, November 19, 2010 10:15 AM by David Wallach

The data from my lookup fields are not coming through - is there somthing special I need to do when using REST?

# EXAM PREP &laquo; Kollipara&#039;s space

Friday, December 24, 2010 8:47 AM by EXAM PREP « Kollipara's space

Pingback from  EXAM PREP &laquo; Kollipara&#039;s space

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Wednesday, January 26, 2011 11:08 PM by chaminda somathilaka

I tried using this to add a list item with a lookup field (multiple allowed). But i failed get values added to the lookup field on the list. Also when I retrieve the list for items that have manually added lookup values, the REST service returned nothing.

Any idea how I can get this to work ?

# MCTS SharePoint 2010 Application Development 70-573 Prep &#8211; Part 1 &laquo; SharePoint Thoughts

Pingback from  MCTS SharePoint 2010 Application Development 70-573 Prep &#8211; Part 1  &laquo; SharePoint Thoughts

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Wednesday, August 10, 2011 9:43 AM by Deep

How to display display name of "Person or Group" type field through jQuery Template?

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Wednesday, August 31, 2011 3:17 PM by Charlie Holland

Excellent write up on ListData.svc. Here's a link to a post that I've done on accessing search via REST, may be useful.

www.chaholl.com/.../sharepoint-2010-search-via-rest.aspx

# $.getJSON, jQuery.tmpl and _vti_bin &laquo; Sharepoint. Sharing the knowledge

Pingback from  $.getJSON, jQuery.tmpl and _vti_bin  &laquo; Sharepoint. Sharing the knowledge

# SharePoint 2010 Accessing Lists the RESTful way &laquo; DotNetAdil

Pingback from  SharePoint 2010 Accessing Lists the RESTful way &laquo; DotNetAdil

# REST and LINQ | SharePoint 2010 | Sharepoint Sriram

Sunday, December 11, 2011 11:35 PM by REST and LINQ | SharePoint 2010 | Sharepoint Sriram

Pingback from  REST and LINQ | SharePoint 2010  | Sharepoint Sriram

# Update list items with listdata.svc &laquo; Sharepoint&#8230;

Thursday, December 15, 2011 2:38 AM by Update list items with listdata.svc « Sharepoint…

Pingback from  Update list items with listdata.svc &laquo; Sharepoint&#8230;

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Monday, December 26, 2011 3:11 AM by Binh Nguyen

Thanks for the great tutorial, just one to contribute a little bit that using ADO.NET data services, not only can you retrieve but also create and update data via REST and ListData.svc in SharePoint 2010. Let refer this link (msdn.microsoft.com/.../ff798339.aspx) if you are interested in. :)  

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Friday, March 2, 2012 3:54 AM by Raj

Hi,

Its a great post.

I tried it and succeeded half way till creating data connection. However, when i used this for a dropdown control, nothing shows up. I am sure i binded repeating section and fields properly for the dropdown.

any help???

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Tuesday, March 13, 2012 12:00 AM by Don

Was having the same problem as several posters with list data requests returning 404 errors. Turns out that the list name is case-sensitive: sp2010/.../Tasks will work but

sp2010/.../tasks will NOT work.

Hope this helps someone...

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Monday, April 2, 2012 10:47 PM by CoreyRoth

@Raj verify that your query is working before doing the binding.

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Monday, April 2, 2012 10:52 PM by CoreyRoth

@Don I have seen the same thing.  List names are directly case sensitive.

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Thursday, August 23, 2012 10:54 AM by Rick

Thank you! This is an excellent alternative to display calendar events for a particular office/division where the calendar resides on another site collection. Instead of writing a farm solution to query other site collections for calendar data, or writing a sandboxed silverlight webpart using web services to query lists outside your site collection, this is simple way to surface list items to users. Thanks for the writeup.

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Wednesday, November 7, 2012 10:00 AM by James Pollard

Installed ADO.NET Data Services 1.5 CTP 2

Rebooted server

Still doesn't work...get 404 error.

Any ideas?

# Filter / Sort / and return JSON with listdata.svc and Sharepoint 2010 &laquo; Casey&#039;s Tech Blog

Pingback from  Filter / Sort / and return JSON with listdata.svc and Sharepoint 2010 &laquo; Casey&#039;s Tech Blog

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Monday, January 14, 2013 5:22 AM by Michal Hainc

Hi, very good explanatory article, but I would need to have something like this article to explain how to  work with the TeamDiscussion collection via the ADO.net DATA web service, on simple "get all" query I am getting exception...

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Monday, March 25, 2013 10:15 AM by Phil

As well as the case sensitivity, the 404 error can be caused by not being able to display the RSS feed due to non-unicode characters in the list text fields

# &raquo; How to Fix a Common SharePoint 2010 OData Error

Wednesday, August 28, 2013 3:48 PM by » How to Fix a Common SharePoint 2010 OData Error

Pingback from  &raquo; How to Fix a Common SharePoint 2010 OData Error

# &raquo; How to Fix a Common SharePoint 2010 OData Error

Wednesday, August 28, 2013 3:48 PM by » How to Fix a Common SharePoint 2010 OData Error

Pingback from  &raquo; How to Fix a Common SharePoint 2010 OData Error

# Fixing the SP2010 IDataServiceUpdateProvider OData Error

Tuesday, September 3, 2013 2:29 PM by Fixing the SP2010 IDataServiceUpdateProvider OData Error

Pingback from  Fixing the SP2010 IDataServiceUpdateProvider OData Error

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Friday, February 7, 2014 7:21 AM by Ajit Kadam

I have tried the URL+_layouts/_vti_bin/ListData.svc but I am getting the Object already exists. Kindly help.

# Using R to connect to a sharepoint list - DexPage

Thursday, June 25, 2015 5:46 PM by Using R to connect to a sharepoint list - DexPage

Pingback from  Using R to connect to a sharepoint list - DexPage

# Sharepoint Caml - DexPage

Saturday, June 27, 2015 5:26 AM by Sharepoint Caml - DexPage

Pingback from  Sharepoint Caml - DexPage

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Thursday, November 17, 2016 5:40 AM by darpan pathak

I want some help in the post method. I want to insert list items with attachments. Can you help me with this ?

Also, from the googling i found out that Sp.JS is very helpful in this manner but i want to insert the data from my ionic application which is not hosted inside the sharepoint.

p.s. : without attachment i am able to send the data and insert the list.

# re: Introduction to querying lists with REST and ListData.svc in SharePoint 2010

Thursday, April 20, 2017 7:42 AM by John

How would I then resolve

<d:CreatedByID m:type="Edm.Int32">1</d:CreatedByID>

to an actual user name of who created the item?

Using SharePoint online (Office 365)

Leave a Comment

(required)
(required)
(optional)
(required)