REST API in SharePoint 2010 for CRUD operation with ListData.svc

REST API in SharePoint 2010 for CRUD Operation 

SharePoint 2010 offers numerous ways to get data from a list. You may use Client Object Model, Linq to SharePoint and REST API by using ListData.svc. In this blog, I will explain how to perform CRUD operation on a SharePoint list using ListData.svc
Generally, SharePoint REST service URL is like this: http://serverurl/_vti_bin/ListData.svc

In this blog, I will be performing CRUD operation with ListData.svc with LINQ query.
So let’s start the implementation.

Create a console application project.
So the very first step is to consume the service, adding a service reference is very similar to Web service and WCF service.

Just browse the service, add the service reference to your project and play with it.
Then add the service reference to your project in Visual Studio.

SharePoint 2010 Rest API

Once service reference added. You may start consuming the service to manipulate SharePoint list data.

Here is C# code for CRUD.


using SPClientTest.CorpSiteService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SPClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Program objProg = new Program();
            objProg.CreateItems();
            objProg.ReadItems();
            objProg.UpdateItems();
            objProg.DeletItems();
        }

      
//Create List item
        private void CreateItems()
        {
            CorpSiteService.MyCorpSiteDataContext serviceCtx = new CorpSiteService.MyCorpSiteDataContext(new Uri(“http://win:2015/_vti_bin/ListData.svc”));
            serviceCtx.Credentials = CredentialCache.DefaultCredentials;

            EmployeeListItem empItem = new EmployeeListItem();
            empItem.Title = “Gopal”;
            empItem.Department = “IT”;
            empItem.City = “Bangalore”;

            serviceCtx.AddToEmployeeList(empItem);
            serviceCtx.SaveChanges();

            Console.WriteLine(“Item Added Successfully”);
        }
 //Read List item
        private void ReadItems()
        {
            CorpSiteService.MyCorpSiteDataContext serviceCtx = new CorpSiteService.MyCorpSiteDataContext(new Uri(“http://win:2015/_vti_bin/ListData.svc”));
            serviceCtx.Credentials = CredentialCache.DefaultCredentials;

            var data = from emp in serviceCtx.EmployeeList
                       select new
                       {
                           Title = emp.Title,
                           Department = emp.Department,
                           City = emp.City,
                       };

            foreach (var item in data)
                Console.WriteLine(item);
        }
 //Update List Item
        private void UpdateItems()
        {
            CorpSiteService.MyCorpSiteDataContext serviceCtx = new CorpSiteService.MyCorpSiteDataContext(new Uri(“http://win:2015/_vti_bin/ListData.svc”));
            serviceCtx.Credentials = CredentialCache.DefaultCredentials;

            EmployeeListItem item = serviceCtx.EmployeeList.Where(listitem => listitem.City == “Mumbai”).FirstOrDefault();

            item.Title = “Ram”;
            item.Department = “New Department”;
            item.City = “Bangalore”;

            serviceCtx.UpdateObject(item);
            serviceCtx.SaveChanges();
            Console.WriteLine(“Item updated successfully”);
        }

//Delete List Item
        private void DeletItems()
        {
            CorpSiteService.MyCorpSiteDataContext serviceCtx = new CorpSiteService.MyCorpSiteDataContext(new Uri(“http://win:2015/_vti_bin/ListData.svc”));
            serviceCtx.Credentials = CredentialCache.DefaultCredentials;

            EmployeeListItem item = serviceCtx.EmployeeList.Where(listitem => listitem.City == “Delhi”).FirstOrDefault();

            serviceCtx.DeleteObject(item);
            serviceCtx.SaveChanges();

            Console.WriteLine(“Item Deleted Successfully”);
        }
     
    }


}

If you want to browse the service details in Visual Studio, then double-click on service reference.
You may see a screen below.

SharePoint 2010 Rest API
You may read some popular blogs on SharePointCafe.Net

1 thought on “REST API in SharePoint 2010 for CRUD operation with ListData.svc”

Leave a Comment

RSS
YouTube
YouTube
Instagram