0

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.

note: 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": []
  }
}
*/ 

(the "?$expand=File" thing yields similar results)

Given I seem to need GetFolderByServerRelativeUrl to replace files, I really need to figure out what's going on there.

potential location of GetFolderByServerRelativeUrl

potential cause of issue folder permission

addendum: Based on some similar issues I'll mention this as well. My app permissions are set as follows: (as suggested here)

 <AppPermissionRequests AllowAppOnlyPolicy="true">
   <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
 </AppPermissionRequests>
C.J. Morrison
  • 21
  • 1
  • 5

2 Answers2

1

I received an answer from the Microsoft form here (https://learn.microsoft.com/en-us/answers/questions/600556/sharepoint-rest-api-getfilebyserverrelativeurl-iss.html)

Though the answer provided by @RaytheonXie-MSFT was close (and probably would had been spot on if I had the right path's in when I first posted, so that's on me)

what I was missing was before GetFolderByServerRelativeUrl call in the url. by adjusting the path there the more relative path will be correct.

what I needed was:

/sites /homePage/DivisionSite/DeptSite/ _api/web/GetFolderByServerRelativeUrl('myLibrary/folder/subFolder')/Files

I was so focused on the GetFolderByServerRelativeUrl command that my path to the path to GetFolderByServerRelativeUrl was wrong

C.J. Morrison
  • 21
  • 1
  • 5
0

The path should be relative path. The url should be like following

/sites/front/_api/web/GetFolderByServerRelativeUrl('Library/folder/otherFolder')/files/add(url='data.json',overwrite=true)

The api is to upload or create a file in the path. You should confirm sharepoint online sites has the folder and document library

  • Thank you for the suggestion, I've updated my question after brushing up on the [correct terminology](https://sharepointmaven.com/difference-between-site-collections-sites-pages-document-libraries-and-folders/) using ```myLibrary/folder/subFolder``` seems to be resulting in a 400 error "Parameter name: Specified value is not supported for the serverRelativeUrl parameter." I was able to shave the route down to down to ```/DeptSite/myLibrary/folder/subFolder``` which I didn't know I could do. However, it gives me the same issue. this matching the link the UI is suggesting (see new img 1). – C.J. Morrison Oct 18 '21 at 23:02