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.
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:
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(); } } }
Create a new list data:
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:
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:
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(); } } }
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: