Friday, September 2, 2011

Try querying lists with REST and ListData.svc in SharePoint 2010

So ready with 1 more.... I just started using the REST for one of my application and found it's pretty good and simple...

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.

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.

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://Server Name/_vti_bin/ListData.svc/

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://Server Name/_vti_bin/ListData.svc/()

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

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://Server Name/_vti_bin/ListData.svc/()/()

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.

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.

No comments: