locked
sharepoint 2013 REST calls RRS feed

  • Question

  • I am looking to create a file on sharepoint server from remote client machine by performing rest call using javascript.  I used the following code.  Seemingly, this should work.  But I am getting Uncaught TypeError: Cannot read property 'majorVersion' of undefined.  It refers to sp.js library.

    <!DOCTYPE html>

    <html>
    <head>
      <title>Office Web Objects Test</title>
    </head>

    <body>
    <script
     type="text/javascript"
     src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js">
    </script>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="http://docs/_layouts/15/sp.js" type="text/javascript"></script>
    <script src="http://docs/_layouts/15/sp.runtime.js" type="text/javascript"></script>
    <script src="http://docs/_layouts/15/sp.requestexecutor.js" type="text/javascript"></script>
    <input id="getFile" type="file"/><br />
    <input id="displayName" type="text" value="Enter a unique name" /><br />
    <input id="addFileButton" type="button" value="Upload" onclick="uploadFile()"/>

    <script>
    function uploadFile() {
      var executor = new SP.RequestExecutor(appweburl);
      var body = "";
      for (var i = 0; i < 1000; i++) {
        var ch = i % 256;
        body = body + String.fromCharCode(ch);
      }
      var info = {
        url: "_api/web/lists/getByTitle('Documents')/Add(url='a.dat', overwrite=true)",
        method: "POST",
        binaryStringRequestBody: true,
        body: body,
        success: success,
        error: fail,
        state: "Update"};
       executor.executeAsync(info);
    }
    </script>

    </body>
    </html>

    Tuesday, April 28, 2015 10:07 PM

Answers

  • Hi,

    According to your description, my understanding is that when you upload documents to document library using Rest API, it throws the error "Cannot read property 'majorVersion' of undefined".

    As the "majorVersion" is existed in the SP.File Object within SP.js file, for a better troubleshooting, I suggest you can debug with Internet Explorer Tool to check if the SP.js has loaded correctly.

    More information:

    Using the F12 Developer Tools to Debug JavaScript Errors

    Here is a detailed code demo for your reference:

    Working with folders and files with REST

    Thanks

    Best Regards


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Proposed as answer by Eric Tao Friday, May 8, 2015 2:07 AM
    • Marked as answer by Eric Tao Saturday, May 9, 2015 8:36 AM
    Wednesday, April 29, 2015 9:27 AM