Sharepoint REST API - GetFileByServerRelativeUrl issue

C.J. Morrison 26 Reputation points
2021-10-22T13:34:28.283+00:00

note: I've also posted this on stack overflow, I will share the answer there if I can get one. All data and images have been "sanitized" to remove any identifying info

For some context, I am using sp-rest-proxy to make a tool which will get, modify and replace json data from SharePoint (MS 365) with a react interface. After figuring out the authentication issues I now have this issue preventing me from updating files.

sp-rest-proxy comes with a very handy interface that let's you test out various routes on localhost, the chunks below are the SharePoint REST relative endpoints. Also, though I am using example paths for privacy purposes, there are no special characters in the real ones, it's not that "%" issue.

This endpoint works great, gives me the data with no fuss.

/sites/front/_api/web/GetFileByServerRelativeUrl('sites/homePage/DivisionSite/DeptSite/myLibrary/folder/subFolder/data.json')/$value   

but these result in 404s

/sites/front/_api/web/GetFolderByServerRelativeUrl('sites/homePage/DivisionSite/DeptSite/myLibrary/folder/subFolder')/files/add(url='data.json',overwrite=true)  
  
/sites/front/_api/web/GetFolderByServerRelativeUrl('sites/homePage/DivisionSite/DeptSite/myLibrary/folder/subFolder')/Files('data.json')  
  
/*  
{  
  "readyState": 4,  
  "responseText": "{\"error\":{\"code\":\"-2147024893, System.IO.DirectoryNotFoundException\",\"message\":{\"lang\":\"en-US\",\"value\":\"File Not Found.\"}}}",  
  "responseJSON": {  
    "error": {  
      "code": "-2147024893, System.IO.DirectoryNotFoundException",  
      "message": {  
        "lang": "en-US",  
        "value": "File Not Found."  
      }  
    }  
  },  
  "status": 404,  
  "statusText": "Not Found"  
}  
*/  

I can use the interface to see that my folder exist and contains items (my json files)

/sites/front/_api/web/GetFolderByServerRelativeUrl('sites/homePage/DivisionSite/DeptSite/myLibrary/folder/subFolder')  
/*  
"..." represents stuff I hid for privacy  
{  
  "d": {  
    "__metadata": {  
      "id": "...",  
      "uri": "...",  
      "type": "SP.Folder"  
    },  
    "Files": {  
      "__deferred": {  
        "uri": "..."  
      }  
    },  
    "ListItemAllFields": {  
      "__deferred": {  
        "uri": "..."  
      }  
    },  
    "ParentFolder": {  
      "__deferred": {  
        "uri": "..."  
      }  
    },  
    "Properties": {  
      "__deferred": {  
        "uri": "..."  
      }  
    },  
    "StorageMetrics": {  
      "__deferred": {  
        "uri": "..."  
      }  
    },  
    "Folders": {  
      "__deferred": {  
        "uri": "..."  
      }  
    },  
    "Exists": true,  
    "IsWOPIEnabled": false,  
    "ItemCount": 5,  
    "Name": "otherFolder",  
    "ProgID": null,  
    "ServerRelativeUrl": "sites/homePage/DivisionSite/DeptSite/myLibrary/folder/subFolder",  
    "TimeCreated": "2021-09-20T18:51:29Z",  
    "TimeLastModified": "2021-10-01T20:03:03Z",  
    "UniqueId": "...,  
    "WelcomePage": ""  
  }  
}  
*/  

However, when I try to view files...

/sites/front/_api/web/GetFolderByServerRelativeUrl('sites/homePage/DivisionSite/DeptSite/myLibrary/folder/subFolder')/Files  
/*  
{  
  "d": {  
    "results": []  
  }  
}  
*/   

My app permissions are set as follows: (as suggested here)

<AppPermissionRequests AllowAppOnlyPolicy="true">  
  <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />  
</AppPermissionRequests>  

as well as some visual that may help.

142928-ss1-2.png
142938-ss2-2.png

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,668 questions
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-10-25T04:00:36.097+00:00

    Hi @C.J. Morrison ,

    Per my test, I could reproduce your issue when retrieving files from the subsite.

    I have a site and the site url is /sites/test, and the file is in a subsite sub1. And if I got the file with the endpoint starting with the root site url like this: /sites/test/_api/web/GetFolderByServerRelativeUrl('/sites/test/sub1/myLibrary/folder/subFolder/')/files

    I got the same result as yours:

    143272-image.png

    To resolve this, we should use the endpoint starting with the subsite(where you files in) url:

    143227-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Luis Fernando Muñoz Velasco 20 Reputation points
    2023-09-28T21:03:41.01+00:00

    Hi

    I'm still having problems trying to get a file from sharepoint api rest, according with sharepoint, the file is here:

    ../sites/Documentos/Documentos%20compartidos/test/OTIF2023CAN.xlsb

    so when I try to run get url like this:

    ..sites/Documentos/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/test')/Files

    I'm getting this:

    User's image

    but the file really is there:

    User's image

    any other option?

    thanks