1

How to read sharepoint list items from a specific folders using Javascript/Jquery through JSOM or REST?

F11
  • 3,703
  • 12
  • 49
  • 83

3 Answers3

0

Using JSOM or CSOM, you need to specify 'FileDirRef' property in CAML Query, and add 'RecursiveAll' scope:

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
    '<View Scope="RecursiveAll"> " +
        "<Query>" +
            "<Where>" +
                "<Eq>" +
                    "<FieldRef Name="FileDirRef" />" +
                    "<Value Type=\"Text\">yourFolderPath</Value>" +
                "</Eq>" +
            "</Where>" +
        "</Query>" +
    "</View>');
Kamil Socha
  • 299
  • 1
  • 10
0
function GetFolders() {
var oWeb = _spPageContextInfo.webAbsoluteUrl;
var URLC = oWeb + "/_api/web/lists/getByTitle('ListName')/items";
$.ajax({
    url: URLC,
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    async: false,
    cache: false,
    success: function (data) {
        $.each(data.d.results, function (index, item) {
            $.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('ListName/ListFolder)/listitemallfields/",
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                async: false,
                cache: false,
                success: function (data) {
                }
            });              
        })
    },
    error: function (data) { }
});

}

0

You can use REST API to read list items from a specific folders using getfolderbyserverrelativeurl.

Refer below Code :

var folderRelativeUrl = "Relative_URL_Of_Your_Folder";  //here specify relative URL of your folder (e.g. '/Shared Documents')


getItemFromFolder().then(getItemFromFolderSuccess, getItemFromFolderFailed);


function getItemFromFolder(){
    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('" + folderRelativeUrl + "')/files?$expand=ListItemAllFields",
        type: "GET",
        headers: {
            "Accept": "application/json;odata=verbose"
        }
    });
}

function getItemFromFolderSuccess(data){
    // success handler
    var response = data.d.results;  // This is Response Object from Server
}

function getItemFromFolderFailed(error){
    // error handler code
} 
Rohit Waghela
  • 844
  • 3
  • 11
  • 21