EDIT: I’ve created a JavaScript API for Sharepoint that handles the file creation. Just look at http://aymkdn.github.com/SharepointPlus/symbols/%24SP%28%29.html#.createFile
Did you know it’s possible to create a file from scratch and to add it into a shared documents library of Sharepoint, and only with Javascript ?
To do it we’ll use the “copy.asmx” web service with the “CopyIntoItems” function.
If you check http://your_sharepoint/_vti_bin/copy.asmx?op=CopyIntoItems you’ll see that we have several details about the “CopyIntoItems” function. Unfortunately it’s very difficult to find any information regarding this on the Web, and specially for Javascript…
So, here is the solution to create a file, e.g. an Excel file (.xls) with a regular HTML table, and we’re going to save it to this library: “http://your_sharepoint/Shared Documents/” (Note: I’m using jQuery for the AJAX requests).
// The file content must be encoded into Base64. To do it I use the function available on my blog (http://blog.kodono.info/wordpress/2011/07/27/midi-code-encoder-decoder-en-base64-pour-javascript-programmation/) function encode_b64(a,b,c,d,e,f){b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";c='=';for(d=f='';e&=3,a.charAt(d++)||(b=';=',e);f+=b.charAt(63&c>>++e*2))c=c<<8|a.charCodeAt(d-=!e);return f} // "Upload" is the name of our function to do the job // txtContent is a plain text, the content of our file // destinationUrl is the full path URL to the document library (with the filename included) function Upload(txtContent, destinationUrl) { var jsStream = encode_b64(txtContent); 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>"+destinationUrl+"</string></DestinationUrls>" +"<Fields><FieldInformation Type='File' /></Fields>" +"<Stream>"+jsStream+"</Stream>" +"</CopyIntoItems>" +"</soap:Body>" +"</soap:Envelope>"; jQuery.ajax({ url: "http://your_sharepoint/_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\"" }); } Upload("<html><table><tr><th>Colonne 1</th><th>Colonne B</th></tr><tr><td>Total:</td><td>1500</td></tr></table></html>","http://your_sharepoint/Shared Documents/test.xls");
Then, when you’ll click on the new “test.xls” file, your web browser will want to open it with Excel. MS Excel should show you a warning message, but just click YES and you’ll see your table inside the sheet !
Of course you can create any type of files (.txt, .jpg, .doc, …) and with any content.
Related links:
It gives 400 – bad request
Since this article I’ve created a JavaScript library that permits to create a file : http://aymkdn.github.com/SharepointPlus/symbols/%24SP%28%29.html#.createFile
I’m going to update the blog post.
Got 400 – bad request too
Charles > did you try to use SharepointPlus ?
Also check with Firebug or the web development toolbar to see the kind of response your receive. You certainly send the wrong information to the server.