npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sp-jsom-node

v4.0.0

Published

SharePoint JavaScript Object Model for Node.js

Downloads

1,666

Readme

sp-jsom-node - SharePoint JavaScript Object Model for Node.js

NPM

npm version Downloads Build Status Gitter chat

sp-jsom-node provides a feasibility of using JSOM (CSOM, SharePoint Client Object Model) in Node.js.

sp-jsom-node patches global variables and request client which let's JSOM used to behave as if it were in it's usual environment - a browser's SharePoint page.

Supported SharePoint versions

  • SharePoint Online, Project Online
  • SharePoint 2019, 2016, Project Server
  • SharePoint 2013
  • SharePoint 2010 [not officially supported]

APIs list

Supported authentication scenarios

  • SharePoint On-Premise (2019, 2016, 2013):

    • User credentials (NTLM)
    • Form-based authentication (FBA)
    • Add-In Only permissions
    • ADFS user credentials
  • SharePoint Online:

    • User credentials (SAML)
    • Add-In Only permissions
    • ADFS user credentials

Installation

npm install sp-jsom-node --save

Usage examples

JsomNode Demo

Minimal setup (TypeScript)

import { JsomNode, IConfigSettings } from 'sp-jsom-node';

export const setting: IConfigSettings = {
  configPath: './config/private.json'
}; // Optional setting, by default ./config/private.json is used

new JsomNode().wizard(setting).then((siteUrl) => {

  /// ... <<< JSOM can be used here

  const ctx = new SP.ClientContext(siteUrl);

}).catch(console.log);

First wizard run propmts for SharePoint site url and credentials strategy parameters.

Minimal setup (JavaScript)

const JsomNode = require('sp-jsom-node').JsomNode;

new JsomNode().wizard().then((siteUrl) => {

  /// ... <<< JSOM can be used here

  const ctx = new SP.ClientContext(siteUrl);

}).catch(console.log);

Initiation with parameters

import { JsomNode, IJsomNodeContext } from 'sp-jsom-node';

const authOptions: any = require('./config/private.json');
const authContext: IJsomNodeContext = {
  siteUrl: authOptions.siteUrl,
  authOptions
};

const ctx = new JsomNode().init(authContext).getContext();

/// ... <<< JSOM can be used here

// const ctx = SP.ClientContext.get_current(); // works with single environment
const oWeb = ctx.get_web();
const oLists = oWeb.get_lists();

const listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_title('New Lists');
listCreationInfo.set_templateType(100);
const oList = oLists.add(listCreationInfo);

ctx.load(oList);
ctx.executeQueryAsync(() => {
  console.log(oList);
}, (sender, args) => {
  console.log(args.get_message());
});

Async/Await usage

Client context runtime is extended with executeQueryPromise - promisified version of executeQueryAsync. Which allows coding with async/await in a "synchronous" handy style, having elegant and easily maintainable code.

import { JsomNode, IJsomNodeContext } from 'sp-jsom-node';

const authOptions: any = require('./config/private.json');
const authContext: IJsomNodeContext = {
  siteUrl: authOptions.siteUrl,
  authOptions
};

(async () => {

  const clientContex = new JsomNode().init(authOptions).getContext();
  // const clientContex = SP.ClientContext.get_current();
  const oListsCollection = clientContext.get_web().get_lists();

  clientContext.load(oListsCollection, 'Include(Title)');
  await clientContext.executeQueryPromise(); // Using JSOM extension

  const listsTitlesArr = oListsCollection.get_data()
    .map((l) => l.get_title());

  console.log('Lists', listsTitlesArr);

})()
  .catch(console.error);

Modules

By default, only core modules are loaded. Additional CSOM features can be requested in modules setting.

Modules list.

import { JsomNode, IJsomNodeSettings } from 'sp-jsom-node';

// ...authOptions

const settings: any = require('./config/private.json');
const jsomSettings: IJsomNodeSettings = {
  modules: [ 'taxonomy', 'userprofiles' ],
  envCode: '2013' // 'spo' is default
};

new JsomNode(jsomSettings).init(authOptions);

/// ... <<< JSOM can be used here

Project server (PM.js)

import { JsomNode, IJsomNodeSettings } from 'sp-jsom-node';

// ...authOptions

const settings: any = require('./config/private.json');
const jsomSettings: IJsomNodeSettings = {
  modules: [ 'project' ]
};

new JsomNode(jsomNodeOptions).init(authOptions);

(async () => {

  // API Reference - https://msdn.microsoft.com/en-us/library/office/jj669820.aspx
  const projCtx = PS.ProjectContext.get_current();
  const projects = projCtx.get_projects();
  projCtx.load(projects, 'Include(Name, Id)');
  await projCtx.executeQueryPromise();

  console.log(projects.get_data().map(p => p.get_name()));

})()
  .catch(console.error);

JSOM Node Settings options

  • modules?: JsomModules[]; // On demand modules load | Default is ['core']
  • envCode?: 'spo' | '2019' | '2016' | '2013'; // Loads different version of JSOM javascripts | Default is 'spo'

Synchronous initiation .init(context: IJsomNodeContext)

  • siteUrl: string; // Optional SPWeb url
  • authOptions: IAuthOptions; node-sp-auth credentials options

Async/wizard initiation .wizard(config?: IConfigSettings)

node-sp-auth-config options

  • configPath?: string; // Path to auth config .json | Default is './config/private.json'
  • encryptPassword?: boolean; // Encrypts password to a machine-bind hash | Default is 'true'
  • saveConfigOnDisk?: boolean; // Saves config .json to disk | Default is 'true'
  • ... see more

Settings can be left blank. Auth options in such a case will be asked by node-sp-auth-config options in a wizard like approach.

Settings scenarios

  • No initial settings (defaults): wizard approach, covers console applications cases with user interaction
  • With explicitly defined authOptions:
    • external tools is in charge for preparing auth credentials in node-sp-auth format
    • credentials should not be dumped on disc
  • Config file with prepopulated credentials: schedule, job automation, continues integration

Integration tests

npm run test

Integration tests

Bundling scripts

When creating automation scripts for production environment, e.g. Azure Job or Function or embedded application like Electron, it can be important to bundle and minify sources with positive performant effect as a result. Check example with bundling.

Inspiration and references

This project was mostly inspired by Vadim Gremyachev's project - CSOMNode, but implements JSOM in node in a bit different way, in TypeScript and supports different auth scenarious implemented in node-sp-auth by Sergei Sergeev.