Creating Azure WebJob To Access SharePoint Using Typescript And Node.js

Hi friends! Today I have brought a post to guide how we can create a web job in Azure to access SharePoint online. We can use different tools to create the web job, but I will use Visual Studio Code to develop it.

Step 1 Create SharePoint Client ID and Client Secret

To access 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 are already installed on the system. If not, then just type the following command.

  • npm install -g

After installing the npm, follow the below steps.

  • Create a folder with the name 'DemoWebJob'.
  • Now, open the folder in the Visual Studio 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 the entries are completed, package.json will be added to the solution.
  • Install some libraries which will help you doing your work. So, type the below commands.
    • npm install typescript
    • npm install sp-request --save-dev
  • Once both the packages get installed, open the package.json and add the new section of engines. Below, I have shown how your package.json will look like.
    1. {  
    2.  "name""demowebjob",  
    3.  "version""1.0.0",  
    4.  "description""Demo project to create web job and access SharePoint data",  
    5.  "main""dist/index.js",  
    6.  "scripts": {  
    7. "test""echo \"Error: no test specified\" && exit 1"  
    8.  },  
    9.  "author""Sumit Kanchan <[email protected]>",  
    10.  "license""MIT",  
    11.  "dependencies": {  
    12. "typescript""^2.5.3"  
    13.  },  
    14.  "devDependencies": {  
    15. "sp-request""^2.1.0"  
    16.  },  
    17.  "engines": {  
    18. "node"">=0.10.0"  
    19.  }  
    20. }  
  • We have installed TypeScript as we will be writing our code using 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 the TypeScript code can be compiled.
    • Execute the command: node_modules\.bin\tsc --init
    • Make the changes to the file, as shown in the image.
      1. {  
      2.  "compilerOptions": {  
      3. "target""es5",  
      4. "module""commonjs",  
      5. "declaration"true,  
      6. "outDir""./dist"  
      7.  },  
      8.  "include": [  
      9. "src/**/*"  
      10.  ]  
      11. }   

Step 3 Writing Code to Access SharePoint

Create a new folder in the solution by the name 'src' and create an 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) in the same folder otherwise the job will fail.

Write the code as written in the image below.

  1. import * as spRequest from 'sp-request';  
  2. import {  
  3.     AppSettings  
  4. } from './Config/Appsettings';  
  5. let spCredentialOptions = {  
  6.     clientId: AppSettings.SPClientID, // Your client ID  
  7.     clientSecret: AppSettings.SPClientSecret // Your Client Secret  
  8. }  
  9. let oRequest = spRequest.create(spCredentialOptions);  
  10. let query: string = `${AppSettings.SPSiteUrl}/_api/web/lists`;  
  11. oRequest.get(query).then((response: any) => {  
  12.     let items = response.body.d.results;  
  13.     items.forEach(item => {  
  14.         console.log(item.Title);  
  15.     });  
  16. });  
To build the solution, execute the following commands -
  1. node_modules\.bin\tsc

  • Once the solution is built, 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.

Up Next
    Ebook Download
    View all
    Learn
    View all