How to Use PnPjs from a Node.js Application: Episode #272

Posted by

Episode #272 – Using PnPjs from a Node.js application

Using PnPjs from a Node.js application

In this episode, we will learn how to use PnPjs, a JavaScript library for working with SharePoint and Microsoft 365, from a Node.js application. PnPjs provides a fluent API that simplifies working with SharePoint and Microsoft 365 data, and can be used in both client-side and server-side applications.

Prerequisites

Before we get started, make sure you have the following installed:

  • Node.js 10.0.0 or later
  • npm (Node Package Manager)
  • An existing Node.js application or project

Installation

To begin using PnPjs in your Node.js application, you can install it via npm:

    
      npm install @pnp/pnpjs
    
  

Usage

Once PnPjs is installed, you can start using it in your Node.js application by importing it and initializing it with the appropriate configurations. Here is an example of how you can use PnPjs to retrieve a list of items from a SharePoint list:

    
      const { sp } = require('@pnp/sp/presets/all');

      sp.web.lists.getByTitle('YourListName').items.get()
        .then((items) => {
          console.log(items);
        })
        .catch((error) => {
          console.error(error);
        });
    
  

With this code, you are using PnPjs to connect to a SharePoint site, retrieve a specific list by its title, and then retrieve all the items in that list. You can then perform any necessary operations on the retrieved items.

Conclusion

Using PnPjs from a Node.js application allows you to easily work with SharePoint and Microsoft 365 data in a server-side environment. By following the installation and usage examples provided in this episode, you can start leveraging the power of PnPjs in your Node.js projects.