Working With SharePoint 2013 REST API in a C# Managed Code

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

  1. using Newtonsoft.Json;  
  2. using Newtonsoft.Json.Linq;  
  3.   
  4.   
  5. namespace REST  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("Site URL/_api/web/lists/getByTitle(List Name')/items");  
  12.           
  13.             endpointRequest.Method = "GET";  
  14.             endpointRequest.Accept = "application/json;odata=verbose";  
  15.             NetworkCredential cred = new System.Net.NetworkCredential("username""password""domain");  
  16.             endpointRequest.Credentials = cred;  
  17.             HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();  
  18.             try  
  19.             {  
  20.                 WebResponse webResponse = endpointRequest.GetResponse();  
  21.                 Stream webStream = webResponse.GetResponseStream();  
  22.                 StreamReader responseReader = new StreamReader(webStream);  
  23.                 string response = responseReader.ReadToEnd();  
  24.                 JObject jobj = JObject.Parse(response);  
  25.                 JArray jarr = (JArray)jobj["d"]["results"];  
  26.                 foreach (JObject j in jarr)  
  27.                 {  
  28.                     Console.WriteLine(j["Title"]+" "+j["Body"]);  
  29.                 }  
  30.                   
  31.                 responseReader.Close();  
  32.                 Console.ReadLine();  
  33.                   
  34.                   
  35.             }  
  36.             catch (Exception e)  
  37.             {  
  38.                 Console.Out.WriteLine(e.Message); Console.ReadLine();  
  39.             }       
  40.         }  
  41.     }  

REST USING XML

  1. using System.Xml;  
  2.   
  3. namespace REST_XML_LIST_GET  
  4. {  
  5.     class Program  
  6.     {  
  7.         static XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable());  
  8.         static Uri sharepointUrl = new Uri("Site URL/");  
  9.         static void Main(string[] args)  
  10.         {  
  11.             xmlnspm.AddNamespace("atom""http://www.w3.org/2005/Atom");  
  12.             xmlnspm.AddNamespace("d""http://schemas.microsoft.com/ado/2007/08/dataservices");  
  13.             xmlnspm.AddNamespace("m""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");  
  14.             NetworkCredential cred = new System.Net.NetworkCredential("username", password", "domain");  
  15.             HttpWebRequest listRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "_api/Web/lists/getByTitle('List Name')/items");  
  16.             listRequest.Method = "GET";  
  17.             listRequest.Accept = "application/atom+xml";  
  18.             listRequest.ContentType = "application/atom+xml;type=entry";  
  19.             listRequest.Credentials = cred;  
  20.             HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse();  
  21.             StreamReader listReader = new StreamReader(listResponse.GetResponseStream());  
  22.             var listXml = new XmlDocument();  
  23.            listXml.LoadXml(listReader.ReadToEnd());  
  24.               
  25.               
  26. //Method 1 Seperate node list  
  27.             var titleList = listXml.SelectNodes("//atom:entry/atom:content/m:properties/d:Title", xmlnspm);  
  28.             var idList = listXml.SelectNodes("//atom:entry/atom:content/m:properties/d:ID", xmlnspm);  
  29.             int i = 0;  
  30.             foreach (XmlNode title in titleList)  
  31.             {  
  32.                 Console.WriteLine(title.InnerXml+" "+idList[i++].InnerXml);  
  33.             }  
  34.   
  35.   
  36.      //Method 2 single node list  
  37.   
  38.             var prop = listXml.SelectNodes("//atom:entry/atom:content/m:properties", xmlnspm);  
  39.             foreach (XmlNode ndlist in prop)  
  40.             {  
  41.                 Console.WriteLine(ndlist.SelectSingleNode("d:Title", xmlnspm).InnerXml + " " + ndlist.SelectSingleNode("d:ID", xmlnspm).InnerXml);  
  42.             }  
  43.             Console.ReadLine();  
  44.   
  45.         }  
  46.     }