3

I have a site which is running under SharePoint 2013. Via REST I am trying to retrieve the file and folder directory for that site. There are several similar questions on this site related to recursively retrieving all of the files and folders for a given a folder:

The answers provided therein led me to the following URI:

/_api/web/Lists/GetByTitle('Documents')/Items?$expand=Folder&$select=Title,FileLeafRef,Folder/ServerRelativeUrl

Which from what I can tell does retrieve all of the files and folders from the given starting path.

However, I am missing something. When reading through the returned XML I cannot figure out where a given file goes. Meaning that I have info on how to retrieve file bros.pdf, but cannot determine that it is in the documents/super/mario/ folder.

For the file items I've tried to retrieve the ServerRelativeUrl property, but have not succeeded though the folders seem to. Is there some identifier in the XML that I am missing? When doing a recursive RESTful query how do I determine where a given file belongs?

ahsteele
  • 131
  • 1
  • 1
  • 6

3 Answers3

5

You are probably looking for FileRef property instead of (or in addition to) FileLeafRef property.

FileRef property returns server relative file/folder url

The following query demonstrates how to retrieve the name and url of every file and folder in Documents library:

/_api/web/Lists/GetByTitle('Documents')/Items?$select=FileLeafRef,FileRef
Vadim Gremyachev
  • 42,498
  • 3
  • 86
  • 167
0

Please expand the File as well, then you can obtain the ServerRelativeUrl property of File.

_api/web/Lists/GetByTitle('Documents')/Items?$expand=Folder,File&$select=Title,FileLeafRef,Folder/ServerRelativeUrl, File/ServerRelativeUrl
Sriram
  • 99
  • 7
0
_spPageContextInfo.webAbsoluteUrl + "/_api/Lists/getbyTitle('" + LibraryName + "')/Items?$select=FileLeafRef,FileRef,Id&$top=5000

use this URL. but after fetching data you need to traverse through FileRef so you can get your exact folder Location. Some custom coding required.

Dikesh Gandhi
  • 6,803
  • 4
  • 30
  • 55
Negi Rox
  • 211
  • 3
  • 9