1

Is it Possible to upload a document to a SharePoint Document library with jquery code?

Is there an equivalent control to asp:fileupload on client side for selecting the file?

Here's the goal:
1) Solution should be a code block I paste in; and not a webpart or compiled solution or App.
2) And I don't want to use the native SharePoint file upload or editform page that comes up to set metadata.

Create a new SharePoint aspx page that:

  • enables users to upload selected file to specific SharePoint document library as a specific hard coded user
  • rename the file to some random unique sequence if :
  • the file does not already exist.
  • the file must be a specific file type (pdf)
  • must be able to set some metadata column values for the file

These links lead me to believe it might be possible:

http://msdn.microsoft.com/en-us/library/ms454491(v=office.14).aspx

http://blog.yahoo.com/lamung/articles/91696

Upload a file to SharePoint through the built-in web services

I will pay for a working solution. Ideally something with only client side code using web services.

Community
  • 1
  • 1
Hell.Bent
  • 1,667
  • 9
  • 38
  • 73

2 Answers2

2

The following code uploads a pdf to document library. This could be of some help to you

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script><script src="/Style%20Library/jquery.SPServices-0.6.2.min.js" type="application/javascript"></script><script src="/Style%20Library/jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
   function uploadFile() {

    var filePath = "c:\\test.pdf";
    var url= "http://Site/Shared Documents/test.pdf";

    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/'> \
        <soap:Body>\
            <CopyIntoItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
                <SourceUrl>" + filePath + "</SourceUrl>\
                    <DestinationUrls>\
                        <string> "+ url + "</string>\
                    </DestinationUrls>\
                    <Fields>\
                        <FieldInformation Type='Text' DisplayName='Title' InternalName='Title' Value='Test' />\
                    </Fields>\
                <Stream>base64Binary</Stream>\
            </CopyIntoItems>\
        </soap:Body>\
    </soap:Envelope>";

    $.ajax({
        url: "http://site/_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\""
    });
    alert(soapEnv);
}


function processResult(xData, status) {
    alert("Uploaded SuccessFully");
}
 </script>
<input name="Upload" onclick="uploadFile()" type="button"/>
Jinxed
  • 738
  • 7
  • 27
  • Are you sure about that? I tested with Firefox and a file is created into the document shared library, but it contains 1KB when my original file is 15KB, and the content is corrupted... – AymKdn Aug 17 '13 at 13:32
1

You can do it with two of my projects:

  1. FileToDataURI: a cross-browser solution to read a file from the user (with FileAPI for the modern browsers and Flash for the old browsers)... it will read the content of the local file
  2. SharepointPlus createFile(): to upload the read content to Sharepoint

I use it for one of my Sharepoint sites (intranet) and it works with all the browsers.

AymKdn
  • 3,327
  • 23
  • 27