siteUrl - SharePoint site (SPWeb) url [string, required]
creds
username - user name for SP authentication [string, optional in case of some auth methods]
password - password [string, optional in case of some auth methods]
Additional authentication options:
Since communication module (sp-request), which is used in sppurge, had received additional SharePoint authentication methods, they are also supported in sppurge.
Callback gets called upon successful file deletion.
errorHandler
Callback gets executed in case of exception inside sppurge. Accepts error object as first argument for callback.
Basic usage example (delete a single file)
constsppurge=require('sppurge').default;constcontext= { /* auth context */ };constoptions= { folder:'/_catalogs/masterpage/spf/module_name', filePath:'/scripts/dummy-file.js'};sppurge(context, options).then(deletionResults => {console.log('A file has been deleted'); }).catch(err => {console.log('Core error has happened', err); });
Basic usage example (delete a folder)
const { Delete } =require('sppurge');constcontext= { /* auth context */ };constsppurge=newDelete();sppurge.deleteFolder(context,'/sites/site/folder/repative/path').then(deletionResults => {console.log('A folder has been deleted'); }).catch(err => {console.log('Core error has happened', err); });
Within Gulp task
constgulp=require('gulp');constwatch=require('gulp-watch'); // Allows more than gulp.watch, is recommendedconstspsave=require('gulp-spsave'); // Optional SPSave, but what is the reason to use SPPurge without SPSave?constsppurge=require('sppurge').default;constpath=require('path');constconfig=require('./gulp.config'); // Getting settings for SPPurge and SPSavegulp.task('watch-assets', () => {returnwatch(config.watch.assets,function (event) {// Base local folder path, e.g. 'src' from which// project's files are mapped to SharePoint foldervar watchBase =config.watch.base;// When file is deleted event value is "unlink"if (event.event ==='unlink') {var sppurgeOptions = { folder:config.sppurge.options.spRootFolder, filePath:path.resolve(event.path).replace(path.resolve(watchBase),'') };// OR:// const sppurgeOptions = {// folder: config.sppurge.options.spRootFolder,// localFilePath: event.path,// localBasePath: watchBase// };sppurge(config.sppurge.context, sppurgeOptions).then(res =>console.log(`File has been deleted: ${res}`)).catch(err =>console.log('Error',err.message)); } else {// Saving files to SharePointgulp.src(event.path, { base: watchBase }).pipe(spsave(// SPSave's core options, see more in spsave documentationconfig.spsave.coreOptions,// node-sp-auth / spsave credential objectconfig.spsave.creds ) ); } });});
Create React App usage scenario
Delete JS's build folder then upload all files from/build folder
One of the architectural decision in CRA is using hashes as a part of assets filenames. This allows avoiding issues related to browser cache. However, it can be challenging in term of deployment to SharePoint assets folders, as all filenames are different on each build. The further sample shows a simple use case approach of deleting files based on folder and name pattern.