Using REST API to access a Sharepoint List


Hello again :)

First of all let’s define what rest stands for ! In fact REST (REpresentational State Transfer) embodies the idea of accessing data across the Internet network, referencing resources using a clear and unique syntax.

Still a bit vague i know !! But you will get it at the end of this post i promise ;)

in this walkthrough we are going to access a sharepoint List from the browser and then from a console application using Visual studio 2010.

So Lets Get Started !!

1. Defining the List:

I almost always use the same list in my post. So here it is again my favorite one Employee Review:

you can create it or use another list as you like.

2. Accessing the list from the browser:

In SharePoint 2010 we can access all SharePoint List  via Restful Service.  Here is the URL to access List REST Service.

http://YourServer/SiteUrl/_vti_bin/ListData.svc.

For Example: my URL becomes : http://win-5g4msqldlt8/_vti_bin/ListData.svc

as you can see here i have all the lists in my site.

so now i want to see a particular list (Employee Review): My URL becomes : http://win-5g4msqldlt8/_vti_bin/ListData.svc/EmployeeReview

See how simple it is !!! and you can do all sorts of queries via URL.

3. accessing the list from a console application:

1. So lets start by opening Visual studio 2010.

2. create a console app project named Employee Review:

3. Right click on the References node in the Solution Explorer window, then click Add Service Reference.

4. Enter http://win-5g4msqldlt8/_vti_bin/ListData.svc for the address.  then click GO and you will have all the lists.

5. Change the namespace from ServiceReference1 to Review.

6. now we are ready to code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using  Employee Review.Review;

namespace Gears
{
class Program
{
static void Main(string[] args)
{
TeamSiteReviewContext dc =
new TeamSiteReviewContext(new Uri(“http://win-5g4msqldlt8/_vti_bin/ListData.svc”));
dc.Credentials = CredentialCache.DefaultNetworkCredentials;
var result = from d in dc.EmployeeReview
select new
{
Name = d.EmployeeName,
Rating = d.Rating,
Comments = d.Comments,
};
foreach (var d in result)
Console.WriteLine(d);
}
}
}

and Voila you will have all the list in you console !! Good job ;)

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s