koltyakov / node-sp-auth-config

🔧 Config options builder for node-sp-auth (SharePoint Authentication in Node.js)
MIT License
20 stars 11 forks source link

minimal code #22

Closed jgranduel closed 5 years ago

jgranduel commented 5 years ago

Hi, As a follow up of this promptly closed issue, may I ask you another favour? I've come across this question on StackOverFlow : PnPJS with on-prem sharepoint?, with no real and useful solution as of 2019. It was posted in 2018, but I think it's still relevant and people as myself still finds it with hope! I'm not self-confident enough to post a complete answer on my behalf. I don't like seeing some sentence like i gave up completely. it was too non-trivial and undocumented. when there are solutions out there! And "sp-pnp-node" is now deprecated in favour of "sp-auth", right?

Would you mind answering or at least tell if the following minimal code is correct and could be posted for who might fight against SharePoint on premise authentication? Thanks again

// minimal code for being up and running with NodeJS.
// Install
mkdir pnp_test
cd  pnp_test
npm init -y

npm install @pnp/logging @pnp/common @pnp/odata @pnp/sp @pnp/graph @pnp/nodejs
npm install pnp-auth
npm install node-sp-auth-config (or npm install -g node-sp-auth-config)

// Configure creds

// npx sp-auth -V
// npx sp-auth -h
npx sp-auth init -p creds_ntlm.json
> npx sp-auth init -p creds_ntlm.json

? SharePoint URL <https://my.sharepoint.onpremise.url>
? Authentication strategy User credentials (NTLM)
? User name <name>
? Domain <domain>
? Password **********************

File saved to .../creds_ntlm.json

// Learn by playing!
> require("esm") // for using modules
import { bootstrap } from 'pnp-auth'
import { AuthConfig } from 'node-sp-auth-config'
import * as pnp from '@pnp/sp' // or import { sp } from "@pnp/sp"

import fetch from "node-fetch"

let authConfig = new AuthConfig({ configPath: "./creds_ntlm.json", encryptPassword: true, saveConfigOnDisk: true })

bootstrap(pnp.sp, authConfig, "<https://my.sharepoint.onpremise.url/path/to/site>" )

// optional, isn't it?
//pnp.sp.setup({ sp: { headers: { Accept: "application/json;odata=verbose" }, baseUrl: "<https://my.sharepoint.onpremise.url/path/to/site>" }})

pnp.sp.web.getFolderByServerRelativeUrl("/site/path/to/any/folder").expand("Folders, Files").get().then(res => console.log(res)).catch(err => console.error(err))
s-KaiNet commented 5 years ago

@jgranduel I will post an update in the mentioned thread since I answered there. Thank you for pointing it out! Things change so quickly and my original answer is not relevant anymore.

jgranduel commented 5 years ago

Merci ! (on behalf of all SP learners!)