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


Here is Part 1

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.

  • Read the list data.
  • Create a new list data.
  • Update the list data.
  • 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:
     
    • Right click on References folder.
    • Click on "Add Service Reference".

      RestApiShare1.gif
       
    • Enter the listdata.svc url and click on Go.

      RestApiShare2.gif
       
    • Enter the namespace and click on Ok.
    • Service Reference will be added successfully.
    • Solution explorer looks like the following.

      RestApiShare3.gif
       
  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(new Uri("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:

  1. 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(new Uri("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:

  1. 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(new Uri("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:

  1. 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(new Uri("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();       
            }
        }
    }

     

Up Next
    Ebook Download
    View all
    Learn
    View all