Creating Azure Web Job to access SharePoint using Typescript and Node.js

HI Friends, today i have bought a post to guide how we can create a webjob in Azure to access SharePoint online. We can use different tools to create the webjob, but i will use Visual Code to develop it.

Step 1: Create SharePoint Client ID and Client Secret

To access the SharePoint from the Azure web job we need to create the SharePoint app through which we can access the data.

To create Client ID navigate to the following url and create a new app
https://<SERVER_NAME>/_layouts/15/appregnew.aspx

To create the app permission and get the client secret:
https://<SERVER_NAME>/_layouts/15/appinv.aspx

Step 2: Configure the solution

To configure the solution, I am assuming that the npm and node.js is already installed on the system. If not then just type the following command:

  • npm install -g
After installing the npm. Follow the below stepas
  • Create a folder with the name 'DemoWebJob'
  • Now open the folder in the Visual code and press 'Cntrl + ~ 
  • Execute the command npm init - (this will create a package.json file)
  • You'll be asked some questions just answer them as shown in the below screen

  • Once all entries are completed package.json will be added to the solution.
  • Install some libraries which will help me doing my work. So type the below commands
    • npm install typescript
    • npm install sp-request --save-dev
  • Once both gets installed open the package.json and add the new section of engines, and below will show how your package.json will look like.
{
"name": "demowebjob",
"version": "1.0.0",
"description": "Demo project to create web job and access SharePoint data",
"main": "dist/index.js",
"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Sumit Kanchan ",
"license": "MIT",
"dependencies": {
  "typescript": "^2.5.3"
},
"devDependencies": {
  "sp-request": "^2.1.0"
},
"engines": {
  "node": ">=0.10.0"
}
}



  • We have installed typescript as we will be writing our code using the typescript. and we have deployed the sp-request as we will be using this library to get connected to SharePoint.
  • Now, we have to create a tsconfig.json file so that typescript can be compiled
    • Execute the command: node_modules\.bin\tsc --init
    • Make the changes to the file as shown in the image
{
"compilerOptions": {
  "target": "es5",                         
  "module": "commonjs",                   
   "declaration": true,                  
   "outDir": "./dist"                    
},
"include": [
  "src/**/*"
]
}



Step 3: Writing Code to Access SharePoint
  • Create a new folder in the solution by the name 'src' and create index.ts file inside the folder. In this folder we will keep all the source files which will get executed when the Job runs. Remember to keep the index.ts at the root of the src and all other files (if any) keep them in the folder otherwise the job will fail.
  • Write the code as written in the image below
import * as spRequest from 'sp-request';
import { AppSettings } from './Config/Appsettings';

let spCredentialOptions = {
  clientId: AppSettings.SPClientID,// Your client ID
  clientSecret: AppSettings.SPClientSecret    // Your Client Secret
}

let oRequest = spRequest.create(spCredentialOptions);

let query: string = `${AppSettings.SPSiteUrl}/_api/web/lists`;

oRequest.get(query).then((response: any) => {

  let items = response.body.d.results;

  items.forEach(item => {
      console.log(item.Title);
  });
});


  • To build the solution execute the command:
    • node_modules\.bin\tsc

  • Once the solution is build copy the node_modules folder inside the dist folder that is created after running the command.
  • To test if the code is able to access the SharePoint execute the following command:
    • node dist\index.js
  • You can see all the lists title are printed on the console.

Step 4: Creating Deployment Package

  1. Select all the files inside the dist folder and compress to .zip file.


Step 5: Deploying to Azure

  • Login to portal.azure.com
  • Go to the existing web app or create a new web app. I will be using the existing web app. 
  • Look for the option Web Jobs
  • Click on the add on the top ribbon and fill in the details. Upload the zip file that we created earlier.
  • Once the job is deployed execute the job and look for the logs.



Hope this tutorial will help you in creating the  Azure Web Jobs to access SharePoint using Typescript.and Node.js

Happy Coding
-Sumit Kanchan

Comments

Popular posts from this blog

SharePoint Framework (SPFx) : Cascade dropdown in webpart properties

Checking user permission in SharePoint Framework (SPFx) webpart

SharePoint Image Gallery