Skip to content

Transfer a file in Sharepoint with Node

Aymeric edited this page Jun 29, 2017 · 1 revision

Node may be useful to transfer documents from one Sharepoint library to another one.

The basic operation will be:

const credentials = {
  "username":"username",
  "password":"password",
  "domain":"domain"
}
const request = require('sp-request').create(credentials); // use https://github.com/s-KaiNet/sp-request
const sp = $SP().auth(credentials);
request({
  url:sourceFileUrl,
  method:'GET',
  encoding:null
}).then(response => {
  if(response.statusCode === 200) {
    // response.body is the ArrayBuffer of our content
    let content = response.body;
    console.log("Uploading « " +filename+ " » ...");
    // upload the file using SharepointPlus
    sp.list(destinationLibrary, destinationUrl).createFile({
      content:content,
      filename:filename
    }).then(file => {
      console.log(" « "+file.Url+" » added!");
    }).catch(err => {
      console.log("Server returned: "+err)
    })
  }
})