Retrieve SharePoint Hosted Add-in Data As JSON From A Console Application Using HTTP REST Request

SharePoint hosted add-in (app) is one of the two ways to create SharePoint add-ins, the other one being Provider Hosted Add-ins. SharePoint hosted add-ins can be used to deploy lists, custom pages, workflows, web parts and other components. We can have only JavaScript code in the pages that are deployed along with SharePoint Hosted add-in. In order to ensure that we have the required templates go to Visual Studio. From Office/SharePoint section check if the ‘SharePoint Add-in’ template is available. If it is not present we will have to install Office developer tools to add the SharePoint 2016 templates along with Office Templates. You can check out how to set up the Visual Studio environment for SharePoint 2016 here.

SharePoint Hosted Add-in Setup

More information on creating and deploying SharePoint Hosted Add-in can be found here. In this article, we will see how to read the data in the SharePoint Hosted Add-in from a console application. This kind of a situation will come in handy when we have to create a windows service or task scheduler that will run on a recurring time period which will read the SharePoint Add-in data and process a logic.

We have created and deployed an Employee List as a SharePoint Hosted Add-in.

SharePoint

We will be making use of the Add-in URL of the SharePoint Hosted Add-in to make a Restful HTTP request from the Console application.

SharePoint

As you can see the Add-in URL is of the format

SharePoint

We can get the Add-in URL which we will use to create the REST URL from the settings page of the Add-in List.

SharePoint

Read the Add-in List data from Console Application

Let’s create a console application from which we will read the Add-in List data.

SharePoint

Since we will be returning the data as JSON, we will have to add a JSON Parser to the project. We will be using Newtonsoft.JSON to parse the returned data from SharePoint. To add it, select NuGet Package Manager.

SharePoint

Search for Newtonsoft.Json from the package manager.

SharePoint

Select the package and click on Install.

SharePoint

This will add the reference to the project.

SharePoint

Add-in Access Code

We will use the below code to issue a REST HTTP request to the SharePoint add-in.REST URL is created by appending the REST API endpoint - "/_api/web/lists/getByTitle('Employee')/items" to the add-in URL - http//holapp-0f5ef7d0145c6e.holapps.com/sites/HOL/SharePoint2016EmployeeAddin.

Once we have created the REST URL we will issue the HTTP request which will fetch us the response stream. We will then create a Response Stream Reader object and read the response stream which will be later converted to JSON array. Once it is converted to JSON we can parse it and get the SharePoint Add-in List data.

Full Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Newtonsoft.Json;  
  7. using Newtonsoft.Json.Linq;  
  8. using System.Net;  
  9. using System.IO;  
  10.   
  11. namespace GetAdd_inDataREST  
  12. {  
  13.     class Program  
  14.     {  
  15.         static void Main(string[] args)  
  16.        {  
  17.   
  18.         string addinURL = "http://holapp-0f5ef7d0145c6e.holapps.com/sites/HOL/SharePoint2016EmployeeAddin";  
  19.         HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(addinURL+"/_api/web/lists/getByTitle('Employee')/items");  
  20.   
  21.         webRequest.Method = "GET";  
  22.         webRequest.Accept = "application/json;odata=verbose";  
  23.         NetworkCredential credentials = new System.Net.NetworkCredential("priyan""password-1""SharePointHOL");  
  24.         webRequest.Credentials = credentials;  
  25.   
  26.         try  
  27.         {  
  28.         //Get the Web Response and Response Strean Object  
  29.         WebResponse HTTPWebResponse = webRequest.GetResponse();  
  30.         Stream responseStream = HTTPWebResponse.GetResponseStream();  
  31.   
  32.         //Create the Response Stream Reader object and read the response stream  
  33.         StreamReader responseStreamReader = new StreamReader(responseStream);  
  34.         string data = responseStreamReader.ReadToEnd();  
  35.   
  36.         //Add the read data to json array  
  37.         JObject jsonData = JObject.Parse(data);  
  38.         JArray jsonArray = (JArray)jsonData["d"]["results"];  
  39.   
  40.         //Get the json array values which is the add-in column values  
  41.         Console.WriteLine("The Add-in Data fetched using REST : ");  
  42.         Console.WriteLine("");  
  43.         foreach (JObject addinData in jsonArray)  
  44.         {  
  45.           Console.WriteLine("Name              : " + addinData["EmployeeName"]);  
  46.           Console.WriteLine("Address           : " + addinData["EmployeeAddress"]);  
  47.           Console.WriteLine("Previous Company  : " + addinData["PreviousCompany"]);  
  48.           Console.WriteLine("-----------------------------------------------");  
  49.         }  
  50.   
  51.         responseStreamReader.Close();  
  52.         Console.ReadLine();  
  53.        }  
  54.        catch (Exception e)  
  55.        {  
  56.         Console.Out.WriteLine(e.Message); Console.ReadLine();  
  57.        }  
  58.   
  59.        }  
  60.     }  
  61. }  

 

Output

SharePoint

Summary

Thus, we saw how to create a console application that will read the SharePoint Hosted Add-in data using an HTTP REST request.