Comments (1)
Hi,
Thanks for the suggestion. It will be available into the next public release. In the meantime here is the new code for $SP().createFile()
(with error
and success
functions ... please look at the parameters: the error
option will have two parameters, the first one will be fileURL
and the second one will be errorMessage
):
/**
@name $SP().createFile
@function
@description Create a file and save it to a Document library
@param {Object} setup Options (see below)
@param {String} setup.content The file content
@param {String} setup.destination The full path to the file to create
@param {Boolean} [setup.encoded=false] Set to true if the content passed is already base64-encoded
@param {String} [setup.url='current website'] The website url
@param {Function} [setup.success=function(fileURL){}] A callback function that will be triggered in case of success; 1 parameter
@param {Function} [setup.error=function(fileURL,errorMessage){}] A callback function that will be triggered in case of failure; 2 parameters
@param {Function} [setup.after=function(fileURL){}] A callback function that will be triggered after the task whatever it's successful or not; 1 parameter
@example
// create a text document
$SP().createFile({
content:"Hello World!",
destination:"http://mysite/Shared Documents/myfile.txt",
url:"http://mysite/",
after:function() { alert("File created!"); }
});
// we can also create an Excel file
// a good way to export some data to Excel
$SP().createFile({
content:"<table><tr><th>Column A</th><th>Column B</th></tr><tr><td>Hello</td><td>World!</td></tr></table>",
destination:"http://mysite/Shared Documents/myfile.xls",
url:"http://mysite/",
after:function() {
window.location.href="http://mysite/Shared Documents/myfile.xls";
}
});
// You can use https://github.com/Aymkdn/FileToDataURI if you want to be able to read a local file
// and then upload it to a document library, via Javascript/Flash
// We'll use "encoded:true" to say our content is alreadu a base64 string
$SP().createFile({
content:"*your stuff with FileToDataURI that returns a base64 string*",
encoded:true,
destination:"http://mysite/Shared Documents/myfile.xls",
url:"http://mysite/"
});
// NOTE: in some cases the file are automatically checked out, so you have to use $SP().checkin()
*/
createFile:function(setup) {
// default values
setup = setup || {};
if (setup.content == undefined) throw "Error 'createFile': not able to find the file content.";
if (setup.destination == undefined) throw "Error 'createFile': not able to find the file destination path.";
setup.url = setup.url || this.url;
// if we didn't define the url in the parameters, then we need to find it
if (!setup.url) {
this._getURL();
return this._addInQueue(arguments);
}
if (setup.url == undefined) throw "Error 'createFile': not able to find the URL!"; // we cannot determine the url
setup.after = setup.after || (function(){});
setup.success = setup.success || (function() {});
setup.error = setup.error || (function() {});
setup.encoded = (setup.encoded==undefined?false:setup.encoded);
var _this=this;
var soapEnv = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+"<soap:Body>"
+"<CopyIntoItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">"
+"<SourceUrl>http://null</SourceUrl>"
+"<DestinationUrls><string>"+setup.destination+"</string></DestinationUrls>"
+"<Fields><FieldInformation Type='File' /></Fields>"
+"<Stream>"+(setup.encoded?setup.content:encode_b64(setup.content))+"</Stream>"
+"</CopyIntoItems>"
+"</soap:Body>"
+"</soap:Envelope>";
jQuery.ajax({
url: setup.url + "/_vti_bin/copy.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
beforeSend: function(xhr) { xhr.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems'); },
contentType: "text/xml; charset=\"utf-8\"",
success:function(data) {
var a = data.getElementsByTagName('CopyResult');
if (a && a[0] && a[0].getAttribute("ErrorCode") !== "Success") {
if (typeof setup.error === "function") setup.error.call(_this, setup.destination, "Error 'createFile': "+a[0].getAttribute("ErrorCode")+" - "+a[0].getAttribute("ErrorMessage"));
} else {
if (typeof setup.success === "function") setup.success.call(_this, setup.destination);
}
if (typeof setup.after === "function") setup.after.call(_this, setup.destination);
},
error:function(qXHR, textStatus, errorThrown) {
if (typeof setup.error === "function") setup.error.call(_this, setup.destination, "Error 'createFile': "+errorThrown);
if (typeof setup.after === "function") setup.after.call(_this, setup.destination);
}
});
return this;
}
from sharepointplus.
Related Issues (20)
- getWorkflowID does not work in NodeJS HOT 6
- stopWorkflow does not work inside NodeJS : ReferenceError: document is not defined HOT 16
- $SP().getUserInfo POST Status 500 error HOT 2
- .get() paging and rowlimit not working for SPO document library HOT 7
- need an example for add.options.packetsize HOT 6
- setReadOnly:Error HOT 3
- Observables with Sharepoint+ HOT 1
- Getting the count of rows from a list HOT 2
- Error from server-side node HOT 4
- addAttachment contextinfo 404 error HOT 19
- Get Column Type HOT 1
- Translate SharePoint column names HOT 2
- distribution list members HOT 2
- Unable to getContentTypeInfo HOT 3
- The site URL passed in the setup object is always ignored in the isMember(...) function HOT 6
- RequestError: Error: tunneling socket could not be established, cause=Client network socket disconnected before secure TLS connection was established HOT 1
- sp.lists gives `ReferenceError: window is not defined` HOT 2
- sharepointplus with sp-rest-proxy HOT 11
- incorrect hasREST function behavior for Sharepoint Online / 365 HOT 2
- joins HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sharepointplus.