s-KaiNet / spsave

Save files in SharePoint using node.js easily
MIT License
86 stars 22 forks source link
javascript sharepoint typescript

spsave

NPM npm version

Need help on SharePoint with Node.JS? Join our gitter chat and ask question! Gitter chat

Nodejs module for saving files in SharePoint:


spsave depends heavily on another module sp-request and use it to send REST queries to SharePoint. sp-request, in turns, depends on the module responsible for SharePoint authentiation from node js - node-sp-auth.
CHANGELOG


How to use:

Install:

npm install spsave --save-dev

Usage:

var spsave = require("spsave").spsave;

spsave(coreOptions, creds, fileOptions)
.then(successHandler)
.catch(errorHandler);

Using with gulp

You have following options:

  1. Use official gulp plugin for spsave - gulp-spsave

  2. Use spsave inside gulp tasks or in watchers like this:

    var spsave = require("spsave").spsave;
    
    gulp.task('spsave', function(cb) {
    
      spsave(coreOptions, creds, fileOptions)
      .then(function(){
          cb();
      }).catch(cb);
    
    );
  3. Use both approaches. First one is handy if you are processing files in a stream, for example you need minimize, concatenate and then upload. The second can be used if you want just upload files and that's it.

Using with SharePoint hosted apps (uploading to app web)

Please refer to this page (at the bottom)

options:

Starting from spsave 3.x all options divided by logical categories (in order):

Core options:

Credentials:

spsave 3.x implicitly depends on another module used for SharePoint authentication from node js - node-sp-auth. For spsave credentials param you need to pass exactly the same object, as for node-sp-auth credentialsOptions object. That also means that spsave supports all authentication options supported by node-sp-auth. On Recipes page you can find a bit more samples.
You can also pass a null as credentials, in that case spsave will ask you for credentials and will store your credentials in a user folder in an encrypted manner (everything is handled by node-sp-auth actually).

File(s) options:

File options used to tell spsave how to find\load the file to be uploaded to SharePoint. When one is used, others are ignored. There are three file options supported: file content, glob and vinyl file.

File content options:
Glob options (you can provide a mask to read all or certain files from the file system):
Vinyl options. If you are familiar with vinyl and vinyl-fs you can provide vinyl file directly:

Don't be scared and confused with a lot of options and take a look at the Recipes page. You can find all possible scenarios with spsave, input params and expected output.

successHandler

Handler gets called upon successful file upload.

errorHandler

Handler gets executed in case of exception inside spsave. Accepts error object as first argument for callback.

Samples

Use Recipes page to see all different options available with spsave.

Basic usage:

var coreOptions = {
    siteUrl: '[sp url]',
    notification: true,
    checkin: true,
    checkinType: 1
};
var creds = {
    username: '[username]',
    password: '[password]',
    domain: '[domain (on premise)]'
};

var fileOptions = {
    folder: 'SiteAssets',
    fileName: 'file.txt',
    fileContent: 'hello world'
};
spsave(coreOptions, creds, fileOptions)
.then(function(){
    console.log('saved');
})
.catch(function(err){
    console.log(err);
});

Development:

I recommend using VS Code for development. Repository already contains some settings for VS Code editor. Before creating Pull Request you need to create an appropriate issue and reference it from PR.

  1. git clone https://github.com/s-KaiNet/spsave.git
  2. cd spsave
  3. git checkout -b myfeature dev
  4. npm run build - restores dependencies and runs typescript compilation
  5. gulp live-dev - setup watchers and automatically runs typescript compilation, tslint and tests when you save files

Tests:

  1. npm test. As a result /reports folder will be created with test results in junit format and code coverage. Additionally test reports will be available in a console window.

Integration testing:

  1. Rename file /test/integration/config.sample.ts to config.ts.
  2. Update information in config.ts with appropriate values (urls, credentials, environment).
  3. Run gulp test-int.