Integrating SharePoint Data Into Node.JS Application

Introduction

In this article, you will learn how to integrate SharePoint applications data into node applications. 

Scenario

Here, let us consider a scenario where you host a node application locally or on cloud platforms. There is some data residing at SharePoint applications or repositories. And the scenario is to pull the data from SharePoint portals in to Node.JS application for processing. SharePoint REST APIs can be used to pull the required information as like SharePoint portals. But the main issue here will be authentication. Since the application is running on Node, the context will be not available for SharePoint authentications to pull the required data. The authentication has to be taken care with the credentials or client secrets.

There are some node packages available for authentications. Let us look into the step by step approach for accomplishing the tasks. 

Prerequisites 
  • Node.JS environment
  • SharePoint online portal (Office 365) 
Steps Involved

Open the command prompt and navigate to the empty folder in your local environment.

Create the Node.JS application by using npm init command from the command prompt (Input the default entries prompted).

 

Once you complete providing inputs, package.json file will be created on the respective folder. The required node packages to be installed for SharePoint authentications will be sp-node-auth and request-promise packages.

Install the packages using npm install commands. The following commands will help install the packages onto the folder. 

  • npm install node-sp-auth --save-dev
  • npm install request-promise --save-dev
getAuth(credentialOptions) method will be used for authenticating to SharePoint sites. In this article sample, the credentialOptions will be directly using the user credentials or service account credentials. 

request.get({url,headers,json}) method will be used to retrieve the SharePoint data into local node Js application.

In the folder create an index.js file manually. In the index.js file, import the necessary packages using required keyword and add the basic code to test the application. The following code snippet shows the sample to retrieve the SharePoint list items.

  1. var http = require('http');  
  2. var spauth = require('node-sp-auth');  
  3. var requestprom = require('request-promise');  
  4. // Site and User Creds  
  5. var url = 'https://nakkeerann.sharepoint.com';  
  6. var username = "abc@nakkeerann.onmicrosoft.com";  
  7. var password = "****";  
  8. var server = http.createServer(function(request, response) {  
  9.     // Authenticate with hardcoded credentials  
  10.     spauth.getAuth(url, {          
  11.         username:username,  
  12.         password:password  
  13.     })  
  14.     .then(function(options){  
  15.         // Headers  
  16.         var headers = options.headers;  
  17.         headers['Accept'] = 'application/json;odata=verbose';  
  18.         // Pull the SharePoint list items  
  19.         requestprom.get({  
  20.         url: url+"/_api/web/lists/getByTitle('customlist1')/items",  
  21.             headers: headers,  
  22.             json: true  
  23.         }).then(function(listresponse){  
  24.             var items = listresponse.d.results;  
  25.             var responseJSON = [];  
  26.             // process  
  27.             items.forEach(function(item) {  
  28.                 if(item.Title !=null){  
  29.                     responseJSON.push(item.Title);  
  30.                 }                     
  31.             }, this);  
  32.             // Print / Send back the data  
  33.             response.end(JSON.stringify(responseJSON));  
  34.               
  35.         });  
  36.     });  
  37.   
  38. });  
  39.   
  40. var port = process.env.PORT || 1337;  
  41. server.listen(port);   

Deploy/ Run

Once you copy paste this code into the index.js file, test the application by running node index.js command in the command prompt. Open the browser and test it with localhost URL and port number specified 1337. (http://localhost:1337)

The following snapshot shows the data being rendered directly on the page.
 
More details about the packages used will be available on the npm site here (https://www.npmjs.com/package/node-sp-auth). 

Summary

Thus you have learned about creating Node JS applications and pulling the data from SharePoint portals. In the next article, we will see how to pull the data from SharePoint with other authentication mechanisms.