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.
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.
As you can see the Add-in URL is of the format
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.
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.
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.
Search for Newtonsoft.Json from the package manager.
Select the package and click on Install.
This will add the reference to the project.
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Net;
- using System.IO;
-
- namespace GetAdd_inDataREST
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- string addinURL = "http://holapp-0f5ef7d0145c6e.holapps.com/sites/HOL/SharePoint2016EmployeeAddin";
- HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(addinURL+"/_api/web/lists/getByTitle('Employee')/items");
-
- webRequest.Method = "GET";
- webRequest.Accept = "application/json;odata=verbose";
- NetworkCredential credentials = new System.Net.NetworkCredential("priyan", "password-1", "SharePointHOL");
- webRequest.Credentials = credentials;
-
- try
- {
-
- WebResponse HTTPWebResponse = webRequest.GetResponse();
- Stream responseStream = HTTPWebResponse.GetResponseStream();
-
-
- StreamReader responseStreamReader = new StreamReader(responseStream);
- string data = responseStreamReader.ReadToEnd();
-
-
- JObject jsonData = JObject.Parse(data);
- JArray jsonArray = (JArray)jsonData["d"]["results"];
-
-
- Console.WriteLine("The Add-in Data fetched using REST : ");
- Console.WriteLine("");
- foreach (JObject addinData in jsonArray)
- {
- Console.WriteLine("Name : " + addinData["EmployeeName"]);
- Console.WriteLine("Address : " + addinData["EmployeeAddress"]);
- Console.WriteLine("Previous Company : " + addinData["PreviousCompany"]);
- Console.WriteLine("-----------------------------------------------");
- }
-
- responseStreamReader.Close();
- Console.ReadLine();
- }
- catch (Exception e)
- {
- Console.Out.WriteLine(e.Message); Console.ReadLine();
- }
-
- }
- }
- }
Output
Summary
Thus, we saw how to create a console application that will read the SharePoint Hosted Add-in data using an HTTP REST request.