PnP-JS-Core (sp-pnp-js) with Node.js made easy
Need help on SharePoint with Node.JS? Join our gitter chat and ask question!
node-pnp-js
allows you to use pnp-js-core
from Node.js environment.
node-pnp-js
implements it's own version of NodeFetchClient
(shipped with sp-pnp-js
) which supports authentication with help of node-sp-auth
module.
How to use
Install
npm install node-pnp-js --save
Import fetch client and configure pnp
;; pnp;
credentials
- the same object (credentialOptions
) provided for node-sp-auth
module. That means you can use any authentication option from node-sp-auth
you want.
Use PnP-JS-Core library in code:
siteUrl
There are three different approaches you can use in order to provide your SharePoint site url.
Web
or Site
constructor (like in a sample above) with siteUrl
constructor param:
1. Use pnp; siteUrl
baseUrl
configuration parameter (coming from sp-pnp-js
):
2. Use pnp; // now you can access your web using chaining syntax // (pnp.sp.web will reference the web with url you provided as baseUrl):pnpspweb
siteUrl
constructor param for NodeFetchClient
:
3. Use pnp; // now you can access your web using chaining syntax // (pnp.sp.web will reference the web with url you provided as siteUrl param):pnpspweb
Use cases:
- Any Node.js project with SharePoint. It can be remote jobs, azure functions, daemons, etc.
- Build pipeline extensibility. You can easily enhance your gulp pipeline with custom actions touching SharePoint.
- Anything else you can do with Node.js and SharePoint :)
Development:
I recommend using VS Code for development. Repository already contains some settings for VS Code editor.
git clone https://github.com/s-KaiNet/node-pnp-js.git
npm install
npm run build
- runs typescript compilation
Integration testing:
- Rename file
/test/integration/private.config.sample.ts
toprivate.config.ts
. - Update information in
private.config.ts
with appropriate values (urls, credentials). - Run
gulp test-int
.