Using PnP JS Core in Node.js

AndrewKoltyakov 588 views 9 slides Jun 09, 2017
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

Using PnP JS Core in Node.js, demo from SPFx and JavaScript SIG community call, June 8th, 2017


Slide Content

Using PnP JS Core in Node.js with help of sp - pnp -node Andrew Koltyakov SharePoint Consultant / Developer at ARVO Systems Email: [email protected] Twitter: @ AndrewKoltyakov

Andrew Koltyakov SharePoint Consultant / Developer at ARVO Systems Email: [email protected] Twitter: @ AndrewKoltyakov Gitter : https://gitter.im/koltyakov About speaker SharePoint guy Node.js guy Love JavaScript (well… TypeScript ) Open source contributor on GitHub https://github.com/koltyakov 06.06.2017 2 Occasional tech blogger http://blog.arvosys.com/author/andrew/ https://www.linkedin.com/in/koltyakov/ Occasional contributor at Microsoft Tech Community sp - pnp -node sp -rest-proxy generator- sppp sp -live-reload node- sp - auth -config sp - jsom -node

Why Node.js / REST API Client on the Server Why Node.js? Really? Server (in current context) is JS environment outside a Browser Client machine Actual servers Consume REST specific features Platform agnostic Standard of API communication “Same” code sharing When to use When Node.js is required =) When the Team is all about JS Build pipeline automation Azure Web Jobs / Functions Desktop clients (i.e. Electron) Microservices You can do awesome things in SharePoint using Node.js / REST and other APIs Node.js is just another option which can live together with .Net and PoSH , etc. 08.06.2017 3

How to make Node.js app talk to SharePoint Choose authentication strategy (SAML, NTLM, FBA, ADFS, Add-In Only) Result of authentication is Authentication Headers (Cookie, Authorization) Inject Auth Headers to the Requests Consume your favorite APIs Works with: SP REST API, SOAP, JSOM/CSOM, getting pages and documents 08.06.2017 4 REST API REST Client Authentication Token/Cookie Should be injected to Request Headers HTTP Request HTTP Response Almost any Node.js HTTP request client module (request, axios , superagent ) + SharePoint authentication library or methods (node- sp - auth ) + Merge Request Headers with Authentication Headers

PnP JS Core Usage experience Started applying the lib in production projects in September 2016 Proved itself in 2013, 2016 and SPO based projects Was one of the inspiration to start using TypeScript (many thanks for this) Unifies projects’ APIs consumption, boosts delivering apps Goes as a default dependency now First ideas of running in Node.js Obsession to re-use once written code / libraries Understanding that a specific app can be written dramatically faster with the lib Try leads to addiction Make it reusable and repeatable sp - pnp -node About library / facts Code name sp-pnp-js Client side library Wraps SharePoint REST API Provides fluent syntax Written in TypeScript Published as NPM module Designed to work in a browser Masters modern approaches No more complements, just try it if you have not yet =) 08.06.2017 5

sp - pnp -node ( PnpNode ) What is it? PnpNode is a drop-in library which provides a quick way of start using PnP JS Core in Node.js application context. Eliminates difference between a browser and Node.js environments for the library “outlook” by implementing SharePoint aware fetch client. Solves authentication issues by integrating node- sp - auth and simplifying configuration layer. Community open project: https://github.com/koltyakov/sp-pnp-node 08.06.2017 6 Installation ```bash npm install sp - pnp -node sp-pnp-js –save ``` Minimal setup ``` javascript ``` import { Web } from ' sp-pnp-js ' ; import { PnpNode } from ' sp-pnp-nod e ' ; ( new PnpNode ()). initAmbient (). then (( settings ) => { const web = new Web ( settings . siteUrl ); // <<< Here goes PnP JS Core code }). catch ( console . log );

Demo sp - pnp -node demonstration and examples 08.06.2017 7

Demo examples “As simple as possible” from scratch to PnP JS Core on the server Installation Minimal code example in JS Ambient and Factory modes, when to use which Ambient mode Factory mode More advanced scenarios Examples in TypeScript Just a variety of different PnP JS Core actions Integration tests Mocha tests and approach Environments Tests execution Running PnP JS Core in TFS Build Environment variables Tests execution in TFS Build Running PnP JS Core in Azure (Web Job) Example job application Configuring app context Running app in Azure Web Job 08.06.2017 8 https://github.com/koltyakov/sp-pnp-node-demo

Thank you! Using PnP JS Core in Node.js with help of sp - pnp -node Andrew Koltyakov SharePoint Consultant / Developer at ARVO Systems Email: [email protected] Twitter: @ AndrewKoltyakov