SPServices: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:
[pre class="brush:jscript" ] $().SPServices({ operation: "GetListItems", async: {true or false}, listName: {list name}, CAMLQuery: {caml query}, CAMLQueryOptions: {caml Query Options}, webURL: {weburl} }); [/pre]
  • 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:
[pre class="brush:jscript" ] var camlQuery = ""; camlQuery += ""; camlQuery += "Yes"; camlQuery += ""; camlQuery += ""; [/pre]
  • 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:
[pre class="brush:jscript" ] var camlQueryOptions = ''; [/pre]
  • 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:

[pre class="brush:jscript" ] 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; } [/pre]
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.

More articles on SPServices:
What is SPServices
How to access List Data using SPServices jQuery library in SharePoint
How to access list of folder name from SharePoint List or Document Library using SPServices jQuery library in SharePoint
How to create/add folder in SharePoint List or Document Library using SPServices jQuery library in SharePoint
Update item in SharePoint List or Document Library using SPServices jQuery library in SharePoint

Post a Comment

Previous Post Next Post