SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the existing SharePoint client object models. Now, developers can interact remotely with SharePoint data using any technology that supports REST web requests. This means that developers can do Create, Read, Update and Delete (CRUD) operations from their apps for SharePoint, solutions and client applications, using REST web technologies and standard Open Data Protocol (OData) syntax.
In this article we can see some examples of accessing SharePoint list data using a REST service
SharePoint 2013 adds the ability for you to remotely interact with SharePoint sites using REST. You can interact directly with SharePoint objects by using any technology that supports standard REST capabilities.
To access SharePoint resources using REST, construct a RESTful HTTP request, using the Open Data Protocol (OData) standard, that corresponds to the desired client object model API as in the following example.
Client object model method:
List.GetByTitle(listname)
REST endpoint:
http://server/site/_api/lists/getbytitle('listname')
The client.svc web service in SharePoint handles the HTTP request and serves the appropriate response in either Atom or JavaScript Object Notation (JSON) format. Your client application must then parse that response. The figure below shows a high-level view of the SharePoint REST architecture.
Create a console application
From the Visual Studio Idle select a console application to start our project. Please provide a location and other details before you create the project
How to install JSON.NET
From the Solution Explorer click on Manage NuGet Packages.You will get the following screen:
Select on Json.Net and click install.
In this article, I am explaining two approaches for accessing a REST service, one using JSON.Net and the other method using XML.
Code to Access REST Using JSON
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
-
-
- namespace REST
- {
- class Program
- {
- static void Main(string[] args)
- {
- HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("Site URL/_api/web/lists/getByTitle(List Name')/items");
-
- endpointRequest.Method = "GET";
- endpointRequest.Accept = "application/json;odata=verbose";
- NetworkCredential cred = new System.Net.NetworkCredential("username", "password", "domain");
- endpointRequest.Credentials = cred;
- HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
- try
- {
- WebResponse webResponse = endpointRequest.GetResponse();
- Stream webStream = webResponse.GetResponseStream();
- StreamReader responseReader = new StreamReader(webStream);
- string response = responseReader.ReadToEnd();
- JObject jobj = JObject.Parse(response);
- JArray jarr = (JArray)jobj["d"]["results"];
- foreach (JObject j in jarr)
- {
- Console.WriteLine(j["Title"]+" "+j["Body"]);
- }
-
- responseReader.Close();
- Console.ReadLine();
-
-
- }
- catch (Exception e)
- {
- Console.Out.WriteLine(e.Message); Console.ReadLine();
- }
- }
- }
- }
REST USING XML
- using System.Xml;
-
- namespace REST_XML_LIST_GET
- {
- class Program
- {
- static XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable());
- static Uri sharepointUrl = new Uri("Site URL/");
- static void Main(string[] args)
- {
- xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom");
- xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
- xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
- NetworkCredential cred = new System.Net.NetworkCredential("username", password", "domain");
- HttpWebRequest listRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "_api/Web/lists/getByTitle('List Name')/items");
- listRequest.Method = "GET";
- listRequest.Accept = "application/atom+xml";
- listRequest.ContentType = "application/atom+xml;type=entry";
- listRequest.Credentials = cred;
- HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse();
- StreamReader listReader = new StreamReader(listResponse.GetResponseStream());
- var listXml = new XmlDocument();
- listXml.LoadXml(listReader.ReadToEnd());
-
-
-
- var titleList = listXml.SelectNodes("//atom:entry/atom:content/m:properties/d:Title", xmlnspm);
- var idList = listXml.SelectNodes("//atom:entry/atom:content/m:properties/d:ID", xmlnspm);
- int i = 0;
- foreach (XmlNode title in titleList)
- {
- Console.WriteLine(title.InnerXml+" "+idList[i++].InnerXml);
- }
-
-
-
-
- var prop = listXml.SelectNodes("//atom:entry/atom:content/m:properties", xmlnspm);
- foreach (XmlNode ndlist in prop)
- {
- Console.WriteLine(ndlist.SelectSingleNode("d:Title", xmlnspm).InnerXml + " " + ndlist.SelectSingleNode("d:ID", xmlnspm).InnerXml);
- }
- Console.ReadLine();
-
- }
- }
- }