REST API in SharePoint 2010 for listdata.svc: Part 2


Introduction:

In SharePoint 2010, we can get the list data using SharePoint object model, client object model or LINQ to SharePoint, we can also get the list data from listdata.svc. We can get the list data from listdata.svc using REST (Representational State Transfer). We can construct URLs in various manners to get specific records, do joins, or perform simple queries. ListData.svc web service will be available in _vti_bin folder of the SharePoint site.

In this article we will be seeing how to create, read, update and delete the list data using REST API.

  1. Read the list data.

  2. Create a new list data.

  3. Update the list data.

  4. Delete the list data.

I have a team site "VJ Testing" which has the "Tasks" list. I am going to read, update, create and delete the data from the "Tasks" list.

Read the list data:

  1. Open Visual Studio 2010.

  2. Go to File => New => Project.

  3. Select the Console Application template from the installed templates and enter the name

  4. Click on Ok.

  5. Add the Service Reference:

a. Right click on References folder.

b. Click on "Add Service Reference".

c. Enter the listdata.svc url and click on Go.


d. Enter the namespace and click on Ok.

e. Service Reference will be added successfully.

f. Solution explorer looks like the following.


6. Add the following namespace

using RESTList.ListDataService;

7. Replace Program.cs with the following code.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using RESTList.ListDataService; using System. Net;

namespace RESTList { class Program { static void Main(string[] args) {

VJTestingDataContext dataContext = new VJTestingDataContext(newUri("http://servername:1111/sites/VJTesting/_vti_bin/listdata.svc/")); dataContext.Credentials = CredentialCache.DefaultNetworkCredentials; var result =from items in dataContext.Tasks select new { Title = items.Title, }; foreach (var item in result) Console.WriteLine(item); Console.ReadLine();    

} } }

8. Build the solution.

9. Hit F5.


Create a new list data:

  • Replace Program.cs with the following code.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using RESTList.ListDataService; using System. Net;

namespace RESTList { class Program { static void Main(string[] args) { VJTestingDataContext dataContext = new VJTestingDataContext(newUri("http://ctsinmbpmoss:1111/sites/VJTesting/_vti_bin/listdata.svc/")); dataContext.Credentials = CredentialCache.DefaultNetworkCredentials; TasksItem taskItem =new TasksItem(); taskItem.Title = "Task3"; dataContext.AddToTasks(taskItem);

dataContext.SaveChanges();        } } }


Update the list data:

  • Replace Program.cs with the following code.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using RESTList.ListDataService; using System. Net;

namespace RESTList { class Program { static void Main(string[] args) { VJTestingDataContext dataContext = new VJTestingDataContext(newUri("http://servername:1111/sites/VJTesting/_vti_bin/listdata.svc/")); dataContext.Credentials = CredentialCache.DefaultNetworkCredentials; TasksItem taskItem = dataContext.Tasks.Where(i => i.Title =="Task1").FirstOrDefault(); taskItem.Title = "NewTask"; dataContext.UpdateObject(taskItem);

dataContext.SaveChanges();        } } }

Delete the list data:

  • Replace Program.cs with the following code.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using RESTList.ListDataService; using System. Net;

namespace RESTList { class Program { static void Main(string[] args) { VJTestingDataContext dataContext = new VJTestingDataContext(newUri("http://servername:1111/sites/VJTesting/_vti_bin/listdata.svc/")); dataContext.Credentials = CredentialCache.DefaultNetworkCredentials; TasksItem taskItem = dataContext.Tasks.Where(i => i.Title =="NewTask").FirstOrDefault(); dataContext.DeleteObject(taskItem);

dataContext.SaveChanges();        } } }

Recent Posts

See All