Giter Club home page Giter Club logo

sp-jsom-node's Introduction

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

NPM

npm version Downloads 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 2016, Project Server
  • SharePoint 2013

APIs list

Supported authentication scenarios

  • SharePoint On-Premise (2013, 2016):

    • 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

Get started

NPM

npm install sp-jsom-node --save

Yarn

yarn add sp-jsom-node

Usage examples

JsomNode Demo

Minimal setup (TypeScript)

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

let jsomNodeOptions: IJsomNodeSettings = {
  // ...
};

(new JsomNode(jsomNodeOptions)).wizard().then((settings) => {

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

  let ctx = SP.ClientContext.get_current();
  // let ctx = SP.ClientContext(webRelativeUrl);

}).catch(console.log);

/// ... <<< JSOM can be used here if config file is already on disk

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((settings) => {

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

  let ctx = SP.ClientContext.get_current();

}).catch(console.log);

Initiation with parameters

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

let settings: any = require('./config/private.json');
let jsomNodeOptions: IJsomNodeInitSettings = {
  siteUrl: settings.siteUrl,
  authOptions: {
    ...(settings as any)
  }
};

(new JsomNode(jsomNodeOptions)).init();

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

const ctx = SP.ClientContext.get_current();
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, IJsomNodeInitSettings } from 'sp-jsom-node';
let jsomNodeOptions: IJsomNodeInitSettings = {
  siteUrl: settings.siteUrl,
  authOptions: {
    ...(settings as any)
  }
};
(new JsomNode(jsomNodeOptions)).init();
(async () => {

  const clientContex = SP.ClientContext.get_current();
  let oListsCollection = clientContext.get_web().get_lists();

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

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

  console.log('Lists', listsTitlesArr);

})();

Modules

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

Modules list.

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

let settings: any = require('./config/private.json');
let jsomNodeOptions: IJsomNodeInitSettings = {
  siteUrl: settings.siteUrl,
  authOptions: {
    ...(settings as any)
  },
  modules: [ 'taxonomy', 'userprofiles' ]
};

(new JsomNode(jsomNodeOptions)).init();

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

Project server (PM.js)

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

let settings: any = require('./config/private.json');
let jsomNodeOptions: IJsomNodeInitSettings = {
  siteUrl: settings.siteUrl,
  authOptions: {
    ...(settings as any)
  },
  modules: [ 'project' ]
};

(new JsomNode(jsomNodeOptions)).init();

(async () => {

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

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

})();

JSOM Node Settings options

  • siteUrl: string; // Optional SPWeb url

  • authOptions: IAuthOptions; node-sp-auth credentials options

  • config?: IAuthConf; 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'
  • modules?: JsomModules[]; // On demand modules load | Default is ['core']

  • envCode?: 'spo' | '16' | '15'; // Loads different version of JSOM javascripts | Default is 'spo'

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.

sp-jsom-node's People

Contributors

koltyakov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.