SharePoint 2010 Rest Interface

Introduction

In this article we will see how to work with SharePoint the REST interface, also known as WCF Data Services (formerly known as ADO.NET Data Services).

We will observe how to query SharePoint list data by using Representational State Transfer (REST) Web service.  SharePoint 2010 implements WCF Data Services for all lists and libraries. This means we can perform operations on list items using simple URIs.

Advantage

WCF Data Services has several advantages over the client object models; they are:

  1. WCF Data Services provides a strongly typed ORM layer that you can use in code to access SharePoint lists and items. In the client object model, all lists and items are returned as more generic types like List and List Item.

  2. WCF Data Services supports LINQ queries against SharePoint lists, whereas the client object model relies on CAML queries.

  3. WCF Data Services uses GET, PUT, POST, and DELETE operations in HTTP so any client that supports HTTP can access SharePoint lists.

Usage

WCF Data Services can be used immediately by simply opening a browser and issuing URI commands.

SharePoint 2010 supports the URI commands through a WCF service named ListData.svc.

ListData.svc is located in the ISAPI directory, that is accessible through URIs containing _vti_bin.

For example: the following URI returns information about all the lists available in "mysite" as XML format:

http://mysitecollection/mysite/_vti_bin/ListData.svc

If you are unable to get XML data or if any error occurs then ensure that the latest ADO.NET Data Services is properly installed.

Accessing List Data

REST Interfaces supports full CRUD operations on the SharePoint lists. After getting the items from the LINQ query, they can be updated and saved. Changes are sent back to the list when we call the SaveChanges method of the context object.

Creating new items and deleting items are performed using the AddTo and DeleteObject methods.

The following is sample C# code shwing how to access list data.

The codes assumes that the custom list "EmpInfo" is created with the columns "FirstName", "LastName" and "EmpId".


Note: Add Service references to ListData.svc. ListDataServiceRef is the proxy class name I have given.
 

ListDataServiceRef.ClientObjectModelDataContext ctxObject =  
new ListDataServiceRef.ClientObjectModelDataContext
(new Uri("http://mysitecollection/mysite/_vti_bin/ListData.svc"));ctxObject.Credentials = CredentialCache.DefaultCredentials;var q = from E in  ctxObject.EmpInfo orderby E.FirstName select E;


The following are other ways of using a service.

Getting only "EmpInfo" list data:

http://mysitecollection/mysite/_vti_bin/ListData.svc/EmpInfo

Getting "EmpInfo" list data with Id equal to "3":

http://mysitecollection/mysite/_vti_bin/ListData.svc/EmpInfo(3)

Getting "EmpInfo" list data results with order by "FirstName" field:

http://mysitecollection/mysite/_vti_bin/ListData.svc/EmpInfo?$orderby=FirstName

Get only specific results from the "EmpInfo" list:

http://mysitecollection/mysite/_vti_bin/ListData.svc/EmpInfo?$top=10
http://mysitecollection/mysite/_vti_bin/ListData.svc/EmpInfo?$skip=2&top=2
http://mysitecollection/mysite/_vti_bin/ListData.svc/EmpInfo?$filter=FirstName eq 'Adi'

Conclusion

Client-side technologies such as the REST interface and client object model are more powerful in SharePoint 2010 than in any previous version of the SharePoint. We should give strong consideration for including them in your SharePoint solutions, especially when displaying data from a list where we need to implement paging and sorting.

Up Next
    Ebook Download
    View all
    Learn
    View all