Tuesday, June 16, 2015

SharePoint 2010 REST Interface

Introduction

In this post we will see how we can work with SharePoint REST interface;also known as WCF Data Services(formerly known as ADO.NET Data Services).
This post provides how to query list data in Microsoft SharePoint Foundation by using Representational State Transfer (REST) Web service.
SharePoint 2010 implements WCF Data Services for all lists and libraries. This means you can perform operations on list items using simple URIs.



Advantage

WCF Data Services has several advantages over the client object models.
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 ListItem.
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 it 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, which is accessible through URIs containing _vti_bin.
Ex: 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 any error comes; ensure that latest ADO.NET Data Services are properly installed or not.

Accessing ListData

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.
Following is the sample c# code how we can

get complete lists data


Add Service references to ListData.svc. ListDataServiceRef is the proxy class name 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;
Following are other ways of service usage

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 ordery by ‘FirstName’ field

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

Get only specific results from ‘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 like REST interface and client object model are more powerful in SharePoint 2010 than in any previous version of the software.
We should give strong consideration to including them in your SharePoint solutions especially while displaying data from list where we need to implement paging and sorting.
Hope you enjoyed one of the good features of sharepoint 2010, happy coding :)

No comments:

Post a Comment