SP 2010's REST api documentation cites an example that uses a list, whereas I wish to rely on a library (for example, "SharedDocuments"), because a standard named library already exists in every employee's personal SP site at my company.

I've managed to http GET read document resources stored in this standard employee personal site library; now I'd like to http POST resources there.

How can I http POST documents to a given library? (May I add that I am evaluating the 2010 SP REST API using Javascript.) As a related matter, how can I create directories in a library with the 2010 SP REST API (using Javascript)?



I have code I use to upload files using the filereader object. The uploadFile function is called on the event change of a file input and passes the event.target.files to the function. If you are using IE you will need to use a polyfill to provide the same functionality. I have used https://github.com/Jahdrien/FileReader.

Event Object:

jQuery('#'  +  element.id).fileReader({
            id: 'fileReaderSWFObject',
            filereader: '/Site/SiteAssets/JS/lib/filereader.swf',
            expressInstall: '/Site/SiteAssets/JS/lib/expressInstall.swf',
            debugMode: true

jQuery('#' +  element.id).on('change', function (event) {

File Upload Function

self.uploadFile = function (files) {
            filereader = {},
            file = {},
            file = files[0];
            filereader = new FileReader();

lename = file.name;
                self.uploadStatus('Reading File');

                filereader.onload = function () {

                    var data = filereader.result,
                    n = data.indexOf(";base64,") + 8;
                    //removing the first part of the dataurl give us the base64 bytes we need to feed to sharepoint
                    data = data.substring(n);
                    self.uploadStatus('Uploading File');
                    jQuery.when(uploadFile(filereader.filename, data)).done(function(results)
                        self.uploadStatus('Upload Complete');

                filereader.onabort = function () {
                    alert("The upload was aborted.");

                filereader.onerror = function () {
                    alert("An error occured while reading the file.");
                //fire the onload function giving it the dataurl


Web Service Call

 function uploadFile(FileName,FileData) {
         //FileData is a byte array   
console.log("uploading file");
            var url= "https://"+location.hostname+"/SiteName/FolderName/"+FileName;

             var soapEnv =
        "<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/'> \
                <CopyIntoItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
                    <SourceUrl>https://"+location.hostname+"/SiteName/FolerName/    </SourceUrl>\
                            <string> "+ url + "</string>\
                            <FieldInformation Type='Text' DisplayName='Title' InternalName='Title' Value='Test' />\
                    <Stream>"+ FileData +"</Stream>\
          return  jQuery.ajax({
                url: "https://"+location.hostname+"/SiteName/_vti_bin/copy.asmx",
                beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"); },
                type: "POST",
                dataType: "xml",
                data: soapEnv,
                complete: processResult,
                contentType: "text/xml; charset=\"utf-8\""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.