Use SPServices to display contents of a single subfolder from a document library

user3711721 picture user3711721 · Jun 20, 2014 · Viewed 7.8k times · Source

In SharePoint and using SPServices, I am trying to view folder contents of a specific subfolder of a document library (and not every single file & folder inside the library). The library structure looks like this:

List Name: Shared Documents

  • Folder #1
    • Subfolder #4
      • File #6
      • File #7
    • Subfolder #5
      • File #8
    • File #9
  • Folder #2
  • Folder #3

Listing out the top-level folders is easy enough with GetListItems but I want to the let the user click on one of the folders and list and list only the direct children of the subfolder.

So, if someone clicks on "Folder #1" then I want to show them only the following:

  • Folder #4
  • Folder #5
  • File #9

I have absolutely no idea how to list out the direct children of a specific subfolder.

Can anyone help?

Answer

Vadim Gremyachev picture Vadim Gremyachev · Jun 21, 2014

In order to restrict query for a specific folder, CAMLQueryOptions parameter with Folder value could be specified:

<QueryOptions>
     <Folder>/SiteName/Lists/Links/folder/subFolder</Folder>
</QueryOptions>

Example

var listName = "Shared Documents";
var folderName = "/Shared Documents/Folder #1";

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: listName,
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    CAMLQueryOptions: "<QueryOptions><Folder>" + folderName + "</Folder></QueryOptions>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var title = $(this).attr("ows_Title");
        console.log(title);
      });
    }
  });

Please follow this thread for a more details.