Introduction
In this article, you will learn how to integrate SharePoint application 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 the npm init command from the command prompt (Input the default entries prompted).
Once you complete providing inputs, the package.json file will be created in 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 credential options 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 the local node Js application.
In the folder create an index.js file manually. In the index.js file, import the necessary packages using the required keyword and add the basic code to test the application. The following code snippet shows the sample to retrieve the SharePoint list items.
var http = require('http');
var spauth = require('node-sp-auth');
var requestprom = require('request-promise');
// Site and User Creds
var url = 'https://nakkeerann.sharepoint.com';
var username = "[email protected]";
var password = "****";
var server = http.createServer(function(request, response) {
// Authenticate with hardcoded credentials
spauth.getAuth(url, {
username: username,
password: password
})
.then(function(options) {
// Headers
var headers = options.headers;
headers['Accept'] = 'application/json;odata=verbose';
// Pull the SharePoint list items
requestprom.get({
url: url + "/_api/web/lists/getByTitle('customlist1')/items",
headers: headers,
json: true
}).then(function(listresponse) {
var items = listresponse.d.results;
var responseJSON = [];
// Process
items.forEach(function(item) {
if (item.Title != null) {
responseJSON.push(item.Title);
}
});
// Print / Send back the data
response.end(JSON.stringify(responseJSON));
});
});
});
var port = process.env.PORT || 1337;
server.listen(port);
Deploy/ Run
Once you copy and paste this code into the index.js file, test the application by running the node index.js command in the command prompt. Open the browser and test it with the 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.