EnumerateFolder with generic lists

Sep 25, 2013 at 10:46 PM
Has anyone found a way to enumerate folders within a generic list?

SiteData EnumerateFolder returns a list of folders and files. It works well with document libraries, but not with generic lists. With generic lists, like those under the Lists folder, it only return file names. No folder names are ever returned. This was tested in both WSS and MOSS 3.0 (SP2007) and appears to be a limitation of the web service. Thank you.

Example page:
<!doctype html>
<html>
<head>
<title>page</title>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2013.01/jquery.SPServices-2013.01.min.js"></script>
</head>
<body>
<script type='text/javascript'>

$(document).ready( function() {

// OK - returns folders and files for document library

    $().SPServices({
        operation: 'EnumerateFolder',
        strFolderUrl: 'Documents',
        completefunc: output
    });

// FAIL - returns files, but not folders for generic list

    $().SPServices({
        operation: 'EnumerateFolder',
        strFolderUrl: 'Lists/Calendar', 
        completefunc: output
    });

})

function output( data ) {
    $('textarea').val( $('textarea').val() + '\n\nOUTPUT:\n' + data.responseText );
}

</script>
<h3>EnumerateFolders</h3>
<textarea style="width:100em;height:40em;padding:1em;"></textarea>
</body>
</html>
Coordinator
Sep 26, 2013 at 9:06 PM
You should be able to get the folders by using GetListItems and filtering for FSObjType=1.

M.
Sep 27, 2013 at 11:30 PM
Thanks Marc. I was not able to make GetListItems return folder info, though it certainly seemed like a good idea. Unfortunately, there's no clear way to discover sub folders within a generic list using web services. However, there is a way to do it using ajax and FrontPage extensions. I've included a demo page below showing how to do this. The data is returned as a list within an HTML page and would need to be parsed (not shown). Happy coding.

Microsoft Reference:
list documents Method
Provides a list of the files, folders, and subsites, complete with meta-information for each file contained in the initialURL Parameter parameter of the specified Web site.

Example:
<!doctype html>
<html>
<head>
<title>page</title>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2013.01/jquery.SPServices-2013.01.min.js"></script>
</head>
<body>
<script type='text/javascript'>

$(document).ready( function() {
    getListFolder( 'Lists/Calendar' );
});
        
/*
param strFolderUrl (string) is the relative url of a list or folder
If FrontPage extensions installed then gets version and requests folder info.
*/

function getListFolder( strFolderUrl ) {

    var webURL = $().SPServices.SPGetCurrentSite();

    $.get( webURL + '/_vti_inf.html', function(data) {
        if (data.indexOf('_vti_bin/_vti_aut/author.dll')>0) {
        
            var FPVersion = data.split('FPVersion="').pop().split('"').shift();

            $.ajax({
                type: 'POST',
                headers: {'X-Vermeer-Content-Type': 'application/x-www-form-urlencoded'},  
                dataType: 'text',
                url:  webURL + '/_vti_bin/_vti_aut/author.dll',
                data: 'method=list+documents:' + FPVersion +
                    '&initialUrl=' + strFolderUrl +
                    '&service_name=/'+
                    // tweak vars below as required
                    '&listHiddenDocs=false'+
                    '&listExplorerDocs=false'+
                    '&listRecurse=false'+
                    '&listFiles=true'+
                    '&listFolders=true'+
                    '&listLinkInfo=true'+
                    '&listIncludeParent=true'+
                    '&listDerived=false'+
                    '&listBorders=false'+
                    '&listChildWebs=true',
                success: function(data) {
                    $('textarea').val( data );
                }
            });
        }
    });
}


</script>
<h4>Folder Data:</h4>
<textarea style="width:100em;height:50em;padding:1em;"></textarea>
</body>
</html>
Coordinator
Oct 3, 2013 at 4:22 PM
Actually, you can do this with GetListItems. It's a matter of setting the scope properly. Here's a simple example:
<script language="javascript" type="text/javascript">
  $(document).ready(function() {
    
    $().SPServices({
      operation:  "GetListItems",
      listName: "foo2",
      CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
      CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='RecursiveAll' /></QueryOptions>",
      completefunc: function (xData, Status) {
        $("#divId").append("<ul>");
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
          var thisFSObjType = $(this).attr("ows_FSObjType").split(";#")[1];
          if(thisFSObjType == 1) {
            $("#divId").append("<li>" +
                $(this).attr("ows_Title") +
              "</li>");
          }
        });
        $("#divId").append("</ul>");
      }
    });
  });
</script>
<div id="divId"/>
M.
Feb 23 at 10:02 PM
For Reference:
For Those looking to Query only Files and exlude Folders.
I used a snippet like this to get only File Items and exlude Folder.
<Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Lookup'>0</Value></Eq></Where></Query>
function SPGetList(myList) {
            $().SPServices({
                operation: "GetListItems",
                async: myList.async,
                webUrl: myList.url,
                CAMLQuery: myList.CAMLQuery,
                CAMLQueryOptions: myList.CAMLQueryOptions,
                listName: myList.listname, //EDIT HERE// This is the Name of the Sharepoint List 
var listMain = {
    //Basic Configurations
    listname: "Library", //Listname as listed in Sharepoint : Change 
    url: "http://mySPServer/Docs/Library/", //Sharepoint Library URL :Change
    isLoaded: false, // Switch to flip after data is loaded  : 'DON'T MODIFY
    JSON: null,
    //Query Options
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Lookup'>0</Value></Eq></Where></Query>", //Get All File Item Exclude Folders
    CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='RecursiveAll' /></QueryOptions>",
    async: true, //run SPServices async: true/false