Share via


How to access list of folder name from SharePoint List or Document Library using SPServices jQuery library in SharePoint

In my previous article, How to access List Data using SPServices jQuery library in SharePoint, I already explained about $().SPServices.GetListItems.

Now, I am going to explain how to get listing of folders including sub-folders in a SharePoint List or Library with SPServices. SPServices JavaScript Library ($().SPServices) provides function called GetListItems to access the data from list.

The GetListItems function details are:

$().SPServices({
        operation: "GetListItems",
        async: <true/false>,
        listName: <List Name>,
        CAMLQuery: <caml Query>,
        CAMLQueryOptions: <caml Query Options>,
        webURL: <webURL>
    });
  • operation: name of the $().SPServices operation is "GetListItems"
  • async: you can pass true or false
  • listName: Name of the list from which data needs to be fetched
  • CAMLQuery: You can mention the CAML query XML with filters. For example, if one of the column name in list is IsAction, then sample CAML query would be:
var camlQuery = "<Query>";
camlQuery += "<Where>";
camlQuery += "<Eq><FieldRef Name='IsActive'/><Value Type='Choice'>Yes</Value></Eq>";
camlQuery += "</Where>";
camlQuery += "</Query>";
  • CAMLQueryOptions: You can mention CAML query options like Recursive. For example, you have to fetch the data from sub-folder also then sample CAML query options would be:
var camlQueryOptions = '<QueryOptions><ViewAttributes Scope="Recursive"/></QueryOptions>';
  •         webURL: This allows you to change the context for the operation to a different site

Now, complete **$().SPServices.**GetListItems operation call  to get listing of folders including the sub-folders would be:

function geFolderListing() {
    var folderPromise = null;
    var objResults = null;
    var listName = 'EmployeeDetails';
 
 
    folderPromise = $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: listName,
    });
 
    folderPromise.success(function () {
        if ($(folderPromise.responseText).find("ErrorText").length > 0) {
            //--If error occured
            objResults = JSON.parse('{ "status": "error", "message": "servicecallError"  }');
            console.log($(folderPromise.responseXML).find("ErrorText").text());
            console.log(objResults);
        }
        else if  ($(folderPromise.responseXML).find("row").length > 0 || $(folderPromise.responseXML).SPFilterNode("z:row").length > 0) {
            //--If success
            var objListItem;
            var listData = [];
            $(folderPromise.responseXML).SPFilterNode("z:row").each(function () {
                objListItem = new  Object();
                objListItem.folder = $(this).attr('ows_Title');
                listData.push(objListItem);
                objListItem = null;
            });
 
            objResults = ({ status: success, message: listData });
            console.log(objResults);
        }
        else {
            //--If error occured
            objResults = JSON.parse('{ "status": "error", "message": "servicecallError::Status Text:In succcess->else block."  }');
            console.log(objResults);
        }
    });
    folderPromise.error(function () {
        if ($(folderPromise.responseXML).find("errorstring").length > 0) {
            //--If error occured
            objResults = JSON.parse('{ "status": "error", "message": "' + $(folderPromise.responseXML).find("errorstring").text() + '"  }');
            console.log(objResults);
        }
    });
    folderPromise.fail(function () {
        if ($(folderPromise.responseXML).find("errorstring").length > 0) {
            //--If error occured
            objResults = JSON.parse('{ "status": "error", "message": "' + $(folderPromise.responseXML).find("errorstring").text() + '::Status Text:' + folderPromise.statusText + '"  }');
            console.log(objResults);
        }
    });
 
    return objResults;
}

With the help of above function, you can get the listing of all folders including sub-folders from the SharePoint list or document library using SPServices JavaScript library