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

SharePoint 2010 offers REST api to work with SharePoint objects. We can perform CRUD operation on SharePoint list using REST service.

Generally SharePoint REST service url is like this : http://serverurl/_vti_bin/ListData.svc



Nothing different, just browse the service add the service reference to your project and play with it.

In this blog I will be performing CRUD operation with ListData.svc with LINQ query.
So lets start the implementation.

Create a console application project.
Then, first step is to add the service reference to your project in Visual studio.



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.




Comments

  1. Very nice blog on REST API in Sharepoint 2010.
    Helped me lot.

    ReplyDelete

Post a Comment

Popular posts from this blog

CAML Query in SharePoint 2010

Calling REST APi from server side code - C#

All about SharePoint 2010 Content Type Hub

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX