none
SharePoint-hosted Add-in Upload File to Document Library in Same Domain RRS feed

  • Question

  • Per requirements, I have created a SharePoint-hosted add-in. I have successfully been able to create folders and save files in a document library using cross-domain, but not on the same domain. The requirements now state that I must upload documents to a library within the add-in. So, I created a document library "MyDocuments" within my add-in structure. I kept all the defaults for the list except for choosing document library option. I have been able to create folder successfully in this library. However, I cannot seem to upload a file or figure out what I am doing wrong?

    The URL for this library is:
    http ://apps-9eef33c6be978a.apps.com/MyApplication/MyDocuments
    (this is SharePoint-created URL for the add-in)

    ERROR MESSAGE

    {"error":{"code":"-2147024809,
    SystemArgumentException","message":{"lang":"en-US","value":"Value
    does not fall within the expected range}}}:400-Bad Request

    CODE

        function addFileToFolder(arrayBuffer){
            var parts = fileInput[0].value.split('\\');
            var fileName = parts[parts.length-1];
            return $.ajax({
                url: "http://apps-9eef33c6be978a.apps.com/MyApplication/_api/web/GetFolderByServerRelativeUrl('/MyDocuments')/Files/add(url='bob.txt',overwrite=true)",
                type: "POST",
                data: arrayBuffer,
                processData: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": "jQuery("#__REQUESTDIGEST").val(),
                    "content-length": arrayBuffer.byteLength
                }
            });
        };

    Monday, April 24, 2017 3:18 PM

Answers

  • My Solution was found here:

    http://sharepoint.sureshc.com/2015/08/How-to-resolve-400-bad-request-error-getFolderByServerRelativeUrl-REST.html

    In case the link is broken, here is what the page states (BIG THANK YOU to Suresh):

    I was working on SharePoint 2013 REST API to fetch the list of folders from a document library specifically a discussion List. The usual lists/getByTitle endpoint gets the list of folders but without the URL of the folder.

    When I checked the REST API examples at MSDN, one thing which caught my attention was getFolderByServerRelativeUrl endpoint. Unfortunately the examples provided in the MSDN is not correct. As per the MSDN, we need to pass the document library name as a parameter.

    executor.executeAsync({ url: "/_api/SP.AppContextSite(@target)/

    web /getfolderbyserverrelativeurl('/Shared Documents')

    ?@target=''", method: "GET", headers:

    { "accept": "application/json; odata=verbose" },

    success: successHandler, error: errorHandler });

    When we try this, we get 400 error (Bad request). So what is the correct way of making it work?

    You need to pass the relative URL of the document library not just only the document library name.

    url: "/_api/SP.AppContextSite(@target)/

    web /getfolderbyserverrelativeurl('/[Managed Path]/[Site]/Shared Documents')

    Example: /sites/SiteA/Shared Documents/
    This works as expected!



    • Edited by Ogrehulk Monday, April 24, 2017 4:43 PM
    • Marked as answer by Ogrehulk Monday, April 24, 2017 4:43 PM
    Monday, April 24, 2017 4:42 PM

All replies

  • My Solution was found here:

    http://sharepoint.sureshc.com/2015/08/How-to-resolve-400-bad-request-error-getFolderByServerRelativeUrl-REST.html

    In case the link is broken, here is what the page states (BIG THANK YOU to Suresh):

    I was working on SharePoint 2013 REST API to fetch the list of folders from a document library specifically a discussion List. The usual lists/getByTitle endpoint gets the list of folders but without the URL of the folder.

    When I checked the REST API examples at MSDN, one thing which caught my attention was getFolderByServerRelativeUrl endpoint. Unfortunately the examples provided in the MSDN is not correct. As per the MSDN, we need to pass the document library name as a parameter.

    executor.executeAsync({ url: "/_api/SP.AppContextSite(@target)/

    web /getfolderbyserverrelativeurl('/Shared Documents')

    ?@target=''", method: "GET", headers:

    { "accept": "application/json; odata=verbose" },

    success: successHandler, error: errorHandler });

    When we try this, we get 400 error (Bad request). So what is the correct way of making it work?

    You need to pass the relative URL of the document library not just only the document library name.

    url: "/_api/SP.AppContextSite(@target)/

    web /getfolderbyserverrelativeurl('/[Managed Path]/[Site]/Shared Documents')

    Example: /sites/SiteA/Shared Documents/
    This works as expected!



    • Edited by Ogrehulk Monday, April 24, 2017 4:43 PM
    • Marked as answer by Ogrehulk Monday, April 24, 2017 4:43 PM
    Monday, April 24, 2017 4:42 PM
  • Hi,

    Glad to hear that you solve this issue, thanks for your sharing.

    It will help others who suck with the problem!

    Best Regards,

    Dennis


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

    Thursday, April 27, 2017 8:08 AM
    Moderator