question

CJMorrison-5581 avatar image
0 Votes"
CJMorrison-5581 asked CJMorrison-5581 commented

Sharepoint REST API - GetFileByServerRelativeUrl issue

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-dev
ss1-2.png (38.8 KiB)
ss2-2.png (33.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MichaelHan-MSFT avatar image
1 Vote"
MichaelHan-MSFT answered CJMorrison-5581 commented

Hi @CJMorrison-5581,

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.





image.png (42.7 KiB)
image.png (55.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you! this completely worked! I had been struggling with it for weeks.

1 Vote 1 ·