(
source code)
Create/Upload a file into a Document library
Parameters:
Object
setup
Options (see below)
ArrayBuffer
setup.content
The file content
String
setup.filename
The relative path (within the document library) to the file to create
Object
setup.fields Optional
If you want to set some fields for the created document
Function
setup.progress Optional, Default: function(percentage){}
The upload progress in percentage
Function
setup.getXHR Optional, Default: function(xhr){}
To manipulate the XMLHttpRequest object used during the upload
Returns:
Promise
resolve(object that represents the file), reject(error)
Example:
$SP().list("Documents", "http://my.other.site/website/").createFile({
content:"*ArrayBuffer*",
filename:"Demo/HelloWorld.txt"
}).then(function(file) {
console.log(file.Url+" has been created")
// and to get more info, like the ID, you can do:
$SP().ajax({url:file.AllFieldsUrl}).then(function(body) {
console.log(body.d)
})
}, function(error) {
console.log("Error: ",error)
})
// create a text document with some fields
$SP().list("Shared Documents").createFile({
content:"ArrayBuffer*",
filename:"SubFolder/myfile.txt",
fields:{
"Title":"My Document",
"File_x0020_Description":"This is my file!"
}
}).then(function(file) {
alert("File "+file.Name+" created at " + file.Url);
});
// use onprogress and abort
$SP().list("Documents").createFile({
content:"*ArrayBuffer*",
filename:"BigFile.iso",
progress:function(perc) {
console.log("percentage of progress => ",perc)
},
getXHR:function(xhr) {
// automtically abort after 3 seconds
setTimeout(function() {
xhr.abort()
}, 3000)
}
}).then(function(file) {
console.log(file.Url+" has been created")
}, function(error) {
console.log("Error: ",error)
})
// example with a input[type="file"]
// <input type="file" id="file_to_upload"> <button type="button" onclick="_uploadFile()">Upload</button>
function _uploadFile() {
var files;
// retrive file from INPUT
files = document.querySelector('#file_to_upload').files;
if (!files || files.length === 0) {
alert("ERROR: Select a file");
return;
}
files = Array.prototype.slice.call(files);
// read the files
Promise.all(files.map(function(file) {
return new Promise(function(prom_res, prom_rej) {
// use fileReader
var fileReader = new FileReader();
fileReader.onloadend = function(e) {
file.content = e.target.result;
prom_res(file);
}
fileReader.onerror = function(e) {
prom_rej(e.target.error);
}
fileReader.readAsArrayBuffer(file);
})
})).then(function(files) {
// upload files
return Promise.all(files.map(function(file) {
return $SP().list("SharepointPlusLibrary").createFile({
content:file.content,
filename:file.name,
progress:function(perc) {
console.log("Progress => ",perc+"%")
}
})
}))
})
}
// if you want to add some headers, for example for authentication method
$SP().list("SharepointPlusLibrary").createFile({
content:file.content,
filename:file.name,
getXHR:function(xhr) {
xhr.setRequestHeader('Authorization','Bearer XYZ')
}
})
// NOTE: in some cases the files are automatically checked out, so you have to use $SP().checkin()
(
source code)
Create a folter in a Document library
Parameters:
String
path
The relative path to the new folder
Returns:
Promise
resolve({BaseName,ID,FSObjType}), reject(error)
Example:
// create a folder called "first" at the root of the Shared Documents library
// the result should be "http://mysite/Shared Documents/first/"
// if the folder already exists, it returns a resolved Promise but with an errorMessage included
$SP().list("Shared Documents").createFolder("first").then(function(folder) { alert("Folder created!"); })
// create a folder called "second" under "first"
// the result should be "http://mysite/Shared Documents/first/second/"
// if "first" doesn't exist then it's automatically created
$SP().list("Documents").createFolder("first/second").then(function(folder) { alert("Folder created!"); }
// Note: To delete a folder you can use $SP().list().remove() with ID and FileRef parameters