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?

3 Answers 3

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
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
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.

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.