SharePoint Asked by jaysonn on October 25, 2021
I have a document library with files and folders. I am using the below code to try to get the list of files inside a specific folder that sits in this document library using the SP REST API.
I am using console.log to try to investigate what I get back but I don’t understand how to find the file names.
REST code:
jQuery.ajax({
url: "https://.../org/eu/EU3/EU34/_api/web/lists/GetByTitle('Goverance Library')/items",
type: "GET",
headers: {"Accept": "application/json;odata=verbose","Content-Type": "application/json;odata=verbose"},
success: function(data) {
console.log(data.d.results);
for(let i = 0; i < data.d.results.length; i++) {
console.log(Object.keys(data.d.results[i]));
}
},
error: function(data) {
console.log("Error occurred trying to get data");
}
});
One of the folders that is inside the library is titled "SOP" but if I try to include it in the code I get a 404 error. The following returns 404.
url: "https://.../org/eu/EU3/EU34/_api/web/lists/GetByTitle('Goverance Library/SOP')/items"
The following is a couple of snippets from what is returned from the console.log.
One of the results from logging data.d.results
:
(100) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0:
AttachmentFiles: {__deferred: {…}}
AuthorId: 7797
CheckoutUserId: null
ContentType: {__deferred: {…}}
ContentTypeId: "0x012000C3ABD68E61A00245B856979F3A10F851"
Created: "2020-07-10T08:31:06Z"
EditorId: 7797
FieldValuesAsHtml: {__deferred: {…}}
FieldValuesAsText: {__deferred: {…}}
FieldValuesForEdit: {__deferred: {…}}
File: {__deferred: {…}}
FileSystemObjectType: 1
FirstUniqueAncestorSecurableObject: {__deferred: {…}}
Folder: {__deferred: {…}}
GUID: "b89a33dd-7b7b-4662-8585-c5283a735676"
ID: 6
Id: 6
Modified: "2020-07-10T08:31:07Z"
OData__CopySource: null
OData__UIVersionString: "1.0"
ParentList: {__deferred: {…}}
RoleAssignments: {__deferred: {…}}
Title: null
__metadata: {id: "Web/Lists(guid'63f6ca95-8834-4aa2-aee2-db93d00a4037')/Items(6)", uri: "https://.../org/eu/EU3/EU34/_api/Web/Lis…d'63f6ca95-8834-4aa2-aee2-db93d00a4037')/Items(6)", etag: ""3"", type: "SP.Data.Goverance_x0020_LibraryItem"}
__proto__: Object
One of the results from logging Object.keys(data.d.results[i])
(24) ["__metadata", "FirstUniqueAncestorSecurableObject", "RoleAssignments", "AttachmentFiles", "ContentType", "FieldValuesAsHtml", "FieldValuesAsText", "FieldValuesForEdit", "File", "Folder", "ParentList", "FileSystemObjectType", "Id", "ContentTypeId", "Title", "ID", "Created", "AuthorId", "Modified", "EditorId", "OData__CopySource", "CheckoutUserId", "OData__UIVersionString", "GUID"]
How can get the list of files inside the folder using REST?
Try to use the GetFolderByServerRelativeUrl
endpoint to return all files and sub-folder from a specific folder URL:
https://server/_api/web/GetFolderByServerRelativeUrl('/Goverance Library/SOP')?$expand=Folders,Files
To return only all files from a specific folder URL you can use:
https://server/_api/web/GetFolderByServerRelativeUrl('/Goverance Library/SOP')?$expand=Files
Check below link for more information: Get all Files and Folders in one call
official documentation: Working with folders and files with REST
Answered by Ganesh Sanap on October 25, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP