On this page you can find different options supported by spsave
.
As you may noted from README, spsave
supports all authentication options node-sp-auth
provides. All you need is to pass appropriate credentials object. Refer to node-sp-auth
credentialsOptions object.
For example SharePoint on-premise addin only authentication with spsave
(consider creds
variable):
var coreOptions = {
siteUrl: '[sp url]'
};
var creds = {
clientId: '[clientId]',
issuerId: '[issuerId]',
realm: '[realm]',
rsaPrivateKeyPath: '[rsaPrivateKeyPath]',
shaThumbprint: '[shaThumbprint]'
};
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);
});
... or SharePoint online user credentials authentication:
var coreOptions = {
siteUrl: '[sp url]'
};
var creds = {
username: '[user@organization.onmicrosoft.com]',
password: '[password]'
};
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);
});
as simple as that.
Lets assume we have core options like siteUrl
and creds. The most interesting options are file content option, so lets take a closer look to all possible scenarios.
As you may know there are three types of file content options - file content, glob, vinyl file.
spsave({
siteUrl: '[sp url]',
checkin: true,
checkinType: 1
},
creds, {
folder: 'SiteAssets',
fileName: 'file.txt',
fileContent: 'hello world'
})
New file file.txt
will be uploaded to [sp site url]/SiteAssets
. If SiteAssets
is document library, file will be visible as new file in document library.
If SiteAssets
folder doesn't exist, spsave
will create that folder. Also the file will be checked in with major version.
spsave({
siteUrl: '[sp url]',
filesMetaData: [{
fileName: 'file.txt',
metadata: {
'__metadata': { type: 'SP.Data.SiteAssetsItem' },
Title: title
}
}]
}, creds, {
folder: 'SiteAssets',
fileName: 'file.txt',
fileContent: 'hello world'
})
New file file.txt
will be uploaded to [sp site url]/SiteAssets
. If SiteAssets
is document library, file will be visible as new file in document library.
If SiteAssets
folder doesn't exist, spsave
will create that folder. Field "Title" will be updated with title provided.
spsave({
siteUrl: '[sp url]',
filesMetaData: [{
fileName: 'Item_Template.js',
metadata: {
'__metadata': { type: 'SP.Data.OData__x005f_catalogs_x002f_masterpageItem' },
Title: 'SPSave Display Template',
DisplayTemplateLevel: 'Item',
TargetControlType: {
'__metadata': {
'type': 'Collection(Edm.String)'
},
'results': [
'SearchResults'
]
},
ManagedPropertyMapping: `'Title':'Title','Path':'Path','Description':'Description'`,
ContentTypeId: '0x0101002039C03B61C64EC4A04F5361F38510660500A0383064C59087438E649B7323C95AF6',
TemplateHidden: false
}
}]
}, creds, {
folder: '_catalogs/masterpage/Display Templates/Search',
fileName: 'Item_Template.js',
fileContent: '<javascript content>'
})
New display template Item_Template.js
will be uploaded to master page gallery. Metadata for the template will be updated.
Save binary file using nodejs fs.readFileSync
function, show notificatin upon upload (or error if any):
spsave({
siteUrl: '[sp url]',
notification: true
}, creds, {
folder: 'SiteAssets/app/templates',
fileName: 'file.txt',
fileContent: fs.readFileSync('file.txt')
})
New file file.txt
will be uploaded to [sp site url]/SiteAssets/app/templates/
. If SiteAssets/app/templates/
doesn't exists, full folders hierarchy wil be created by spsave
spsave({
siteUrl: '[sp url]'
}, creds, {
glob: 'myapp/files/file.txt',
folder: 'SiteAssets'
})
New file file.txt
will be uploaded to [sp site url]/SiteAssets
spsave({
siteUrl: '[sp url]'
}, creds, {
glob: 'myapp/data/files/file.txt',
folder: 'SiteAssets',
base: 'myapp'
})
New file file.txt
will be uploaded to [sp site url]/SiteAssets/data/files
(because of base
option)
spsave({
siteUrl: '[sp url]'
}, creds, {
glob: 'myapp/data/files/file.txt',
base: 'myapp'
})
New file file.txt
will be uploaded to [sp site url]/data/files/
(because of folder
is missing, data
becomes root folder)
spsave({
siteUrl: '[sp url]'
}, creds, {
glob: 'myapp/**/*.*',
base: 'myapp',
folder: 'SiteAssets'
})
All files from myapp
local folder will be uploaded to SiteAssets
folder. spsave
will preserve folder structure if myapp
has any subfolders.
spsave({
siteUrl: '[sp url]'
}, creds, {
glob: 'myapp/**/*.*',
folder: 'SiteAssets'
})
All files from myapp
local folder will be uploaded to SiteAssets
folder using flatten structure (base
option is missing).
spsave({
siteUrl: '[sp url]'
}, creds, {
glob: ['myapp/files/file.txt', 'myapp/templates/home.html'],
folder: 'SiteAssets'
})
Two files will be uploaded to [sp site url]/SiteAssets
- file.txt
and home.html
var vfs = require('vinyl-fs');
var map = require('map-stream');
vfs.src('files/file.txt')
.pipe(map(function(file, cb) {
spsave({
siteUrl: '[sp url]'
}, creds, {
file: file,
folder: 'SiteAssets'
})
.then(function(){ console.log('success'); })
.finally(function(){ cb(); });
New file file.txt
will be uploaded to [sp site url]/SiteAssets
var vfs = require('vinyl-fs');
var map = require('map-stream');
vfs.src('myapp/data/files/file.txt', { base: 'myapp' })
.pipe(map(function(file, cb) {
spsave({
siteUrl: '[sp url]'
}, creds, {
file: file,
folder: 'SiteAssets'
})
.then(function(){ console.log('success'); })
.finally(function(){ cb(); });
New file file.txt
will be uploaded to [sp site url]/SiteAssets/data/files