0

I am able to retrieve list and list items just fine:

var client2 = new RestClient("https://preactdk.sharepoint.com/sites/mysite/_api/web/lists/getbytitle('mylist')/items");
client2.Timeout = -1;
var request2 = new RestRequest(Method.GET);
request2.AddHeader("Authorization", $"Bearer {tokenData.access_token}");
request2.AddHeader("Accept", "application/json;odata=verbose");
IRestResponse response2 = client2.Execute(request2);

However, when I try to retrieve the file within "Documents" in the "testfolder" directory via _api/web/GetFolderByServerRelativeUrl('testfolder/asdf.txt')/Files I get the following result:

{
    "d": {
        "results": []
    }
}

What exactly am I doing wrong?

I wish to retrieve the URL of the asdf.txt file & then download it.

2 Answers 2

0

You should use the REST endpoint in below format using GetFileByServerRelativeUrl instead of GetFolderByServerRelativeUrl:

https://{site_url}/_api/web/GetFileByServerRelativeUrl('/Folder Name/{file_name}')

Example:

Endpoint:

https://tenant.sharepoint.com/sites/SPConnect/_api/web/GetFileByServerRelativeUrl('/sites/SPConnect/Shared%20Documents/Document.docx')

Output:

<m:properties>
    <d:CheckInComment></d:CheckInComment>
    <d:CheckOutType m:type="Edm.Int32">2</d:CheckOutType>
    <d:ContentTag>{03DE5AB8-5EA1-4F30-85A0-D6CDA53CA8EF},14,17</d:ContentTag>
    <d:CustomizedPageStatus m:type="Edm.Int32">0</d:CustomizedPageStatus>
    <d:ETag>"{03DE5AB8-5EA1-4F30-85A0-D6CDA53CA8EF},14"</d:ETag>
    <d:Exists m:type="Edm.Boolean">true</d:Exists>
    <d:IrmEnabled m:type="Edm.Boolean">false</d:IrmEnabled>
    <d:Length m:type="Edm.Int64">27544</d:Length>
    <d:Level m:type="Edm.Byte">1</d:Level>
    <d:LinkingUri>https://tenant.sharepoint.com/sites/SPConnect/Shared%20Documents/Document.docx?d=w03de5ab85ea14f3085a0d6cda53ca8ef</d:LinkingUri>
    <d:LinkingUrl>https://tenant.sharepoint.com/sites/SPConnect/Shared Documents/Document.docx?d=w03de5ab85ea14f3085a0d6cda53ca8ef</d:LinkingUrl>
    <d:MajorVersion m:type="Edm.Int32">6</d:MajorVersion>
    <d:MinorVersion m:type="Edm.Int32">0</d:MinorVersion>
    <d:Name>Document.docx</d:Name>
    <d:ServerRelativeUrl>/sites/SPConnect/Shared Documents/Document.docx</d:ServerRelativeUrl>
    <d:TimeCreated m:type="Edm.DateTime">2020-01-29T08:00:16Z</d:TimeCreated>
    <d:TimeLastModified m:type="Edm.DateTime">2020-02-25T06:49:58Z</d:TimeLastModified>
    <d:Title></d:Title>
    <d:UIVersion m:type="Edm.Int32">3072</d:UIVersion>
    <d:UIVersionLabel>6.0</d:UIVersionLabel>
    <d:UniqueId m:type="Edm.Guid">03de5ab8-5ea1-4f30-85a0-d6cda53ca8ef</d:UniqueId>
</m:properties>

Documentation: Working with folders and files with REST

6
  • Thank you so much. That solved it. I also use the header Accept: application/json;odata=verbose. Can you check this result with this header? how do I then download the actual file? there should be a way to build a URL or something
    – crystyxn
    Jun 9, 2021 at 15:06
  • when I try without that header to get XML back, my <d:LinkingUri> and <d:LinkingUrl> are empty
    – crystyxn
    Jun 9, 2021 at 15:19
  • Edit: found it, have to add /$value at the end
    – crystyxn
    Jun 9, 2021 at 15:23
  • You're right. You need to use /$value to get the file contents/value. Jun 9, 2021 at 15:46
  • 1
    Are you downloading the file via script or on button/link click from browser? You need to provide the filename & extension. check examples at here. Also, alternative way to download file is to pass file URL to http://siteUrl/_layouts/download.aspx?SourceUrl=<fileUrl>. Check this for more information. Jun 9, 2021 at 16:06
1

I had the same issue when I used "siteAbsoluteUrl".

let siteUrl = _spPageContextInfo.siteAbsoluteUrl;
let url = siteUrl + "/_api/web/getFolderByServerRelativeUrl('<relative path to the folder>')/Files";

When I use "webAbsoluteUrl", then it gives me the files from the folder.

let siteUrl = _spPageContextInfo.webAbsoluteUrl;
let url = siteUrl + "/_api/web/getFolderByServerRelativeUrl('<relative path to the folder>')/Files";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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