How to retrieve the SharePoint List Item using Express JS

Krishna KV
 
Team Leader, Aspire Systems
March 5, 2017
 
Rate this article
 
Views
5545

Express JS is a node based web server for web and mobile applications. In this article we can have look into how to retrieve the SharePoint list items via Express Js

Key Features

· It is quick and easy setup.

· It support cross platform.

Middleware

The middleware is a chain of function to handle requests and response. Middleware in Express Js is used to configure routing, security, models and views.

clip_image001

Prerequisite

1. Node JS

2. Visual Studio Code.

Create a folder (EJS) and open it with visual studio code. Execute “run npm init” in the integrated console of VS Code. Replace the “package.json” file with the one displayed below.

package.json

 {
     "name": "exp",
     "version": "1.0.0",
     "main": "index.js",
     "scripts": {
         "start": "node src/express.js"
     },
     "dependencies": {
         "express": "^4.14.1",
         "body-parser": "^1.16.1",
         "compression": "^1.6.2",
         "httpntlm": "1.7.4" ,
         "lodash": "4.17.4"
     }
 }

Add src file and include the doc.js and express.js

doc.js

 const express = require('express');
 let router = express.Router();
 let httpntlm = require('httpntlm');
 
 let config = {
     url: "http://sharepointurl/_api/web/lists/getByTitle('Testing')/items?$select=Title,ID",
     username: 'user',
     password: 'password',
     domain: 'domain',
     workstation: '',
     headers: {
         "Accept": "application/json;odata=verbose",
         "content-type": "application/json;odata=verbose;"
     }
 };
 
 router.get('/', (req, res, next) => {
     let httpResponse = { "statusCode": 500, result: undefined };
     httpntlm.get(config,
         function success(error, response) {
             httpResponse.result = JSON.parse(response.body);            
             res.status(200).send(httpResponse.result.d.results);
 
         });
 });
 
 module.exports = router;

app.js

 const express = require('express');
 const bodyParser = require('body-parser');
 const compression = require('compression');
 let port = 500;
 let app = express();
 let router = express.Router();
 
 app.use(compression());
 app.use(bodyParser.json());
 app.use('/api', router); // Adding router middleware to express js 
 
 let doc = require('./doc');
 router.use('/doc', doc);
 
 app.listen(port, (err, req, res, next) => {
     console.log(`server started at ${port}`);
 });

To run the api.

npm install

npm start

Author Info

Krishna KV
 
Team Leader, Aspire Systems
 
Rate this article
 
Krishna K.V has been working in IT Industry for over 7+ years. He holds a Master Degree in Information Technology. He is more interested to learn and share new technologies, ...read more
 

Leave a comment