0

Attempting to retrieve a file from Sharepoint using guidance from this page

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest#working-with-files-by-using-rest

generally works.

I can access these types of files programmatically(download them locally): "server/sites/site/folder/subfolder/myfile.xlsx"

However: "server/sites/site/folder/_layouts/15/Doc.aspx?sourcedoc=entry&file=myfile.xlsx"

has beeen annoyingly inaccessible for me - all I get is:

HTTP/1.1 401 Unauthorized

I can access the file in my browser without any issues. What am I missing here? I cannot find any references in the documentation nor online on what else to try here.

It is a requirement that I retrieve this file with a REST request and to download them locally.

0

Solution 1:

I personally prefer to pass this work to sharepoint and use the url something like below:

http://spsite/_layouts/download.aspx?SourceUrl=http://spsite/Documents/sample.docx

Solution 2:

You can set the binaryStringResponseBody to true to downstream a file from SharePoint.

var info = {
    url: appwebUrl + "/_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('/sites/Test/Shared Documents/" + sourcefile.get_name() + "')/$value?@target='myhosturl'",

    binaryStringResponseBody: true,
    success: function (data) {
    },
    error: function () { alert('error'); }

}

If you're facing any issue please follow the below link: Get file using REST in SharePoint 2013/Office 365.

Solution 3:

If you’re using SharePoint add-in/app, you could get file binary data by REST api.

_api/Web/GetFileByServerRelativeUrl('/Doc1/Lee1.docx')/$value

Source: How to download file from document Library using Rest API.

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.