Listing Folders in a SharePoint List or Library with SPServices

I got a question in the SPServices Discussions the other day (it’s currently at the bottom of the thread) about listing folders in a SharePoint Document Library with SPServices. Little examples like this can be useful learning tools, even if they aren’t exactly what you are trying to do.

<UPDATE>Based on the first two comments I received below, I want to point out that this post is just to show something you might do with SPServices. It’s not efficient, nor does it yield a result that is useful. I intended it as yet another example for SPServices, which you might use in some way after adapting it to your needs, but certainly not “as is”!</UPDATE>

Here’s a revised version of the code I came up with. Note that I’ve formatted it to display well here, so it spreads across more lines than I’d usually use. The output is also a little silly, but it’s just to demonstrate how this works.

<script language="javascript" type="text/javascript" src="jQuery%20Libraries/jquery-1.5.min.js"></script>
<script language="javascript" type="text/javascript" src="jQuery%20Libraries/jquery.SPServices-0.6.0.min.js"></script>
<script language="javascript" type="text/javascript">
  $(document).ready(function() {

    $().SPServices({
      operation:  "GetListItems",
      listName: "jQuery Libraries",
      completefunc: function (xData, Status) {
        $("#divId").append("<ul>");
        $(xData.responseXML).find("[nodeName='z:row']").each(function() {
          var thisFSObjType = $(this).attr("ows_FSObjType").split(";#")[1];
          if(thisFSObjType == 1) {
            $("#divId").append("<li>" +
            $(this).attr("ows_FileLeafRef").split(";#")[1] +
              "  [" +
                $(this).attr("ows_FileLeafRef") +
                " :: " +
                $(this).attr("ows_FSObjType") +
              "]" +
            "</li>");
          }
        });
        $("#divId").append("</ul>");
      }
    });
  });
</script>
<div id="divId"/>
Folder Listing
Folder Listing

Folders are items in lists just like any other items, but they have some unique attributes. Because of this, we can use GetListItems as we would to get any other types of items from a list. I’m making a simple call to GetListItems for the “jQuery Libraries” list, which happens to be where I store all of my — you guessed it — jQuery libraries. In that document Library, I have two folders: “Older”, where I store older versions of things, and “Other”, where I store other stuff. Inventive, eh? The output from this code is shown at the right.

In the completefunc, I’m processing the returned XML from the GetListItems funcxtion as follows:

  • Line 10: I’m starting an unordered list to contain the results
  • Line 11: Here’s where I look for all of the individual items returned from the GetListItems call. Each of them is contained in an XML element called “z:row”.
  • Line 12: I grab the value of the FSObjType attribute and assign it to a variable called thisFSObjType. All column names are prefixed with “ows_” (Office Web Services?) in the results from GetListItems. This is sort of weird, but you get used to it. The value in the ows_FSObjType attribute looks like “42;#1”, as you can see in the output above. This means that it is the item with its ID=42, which has the FSObjType=1.
  • Line 13: Folders have an FSObjType=1, so I test for that here. All “regular” items have FSObjType=0.
  • Lines 14-21: These lines sinply format information about the folder item for output. I’m showing the folder’s name, and in the brackets the raw values for the FileLeafRef (The DisplayName for this column is “Name”, which you see all the time in SharePoint Document Libraries.) and FSObjType columns.
  • Line 24: Closes off the unordered list

Hopefully this little post is helpful to see something else you might be able to do with SPServices and the Lists Web Service. If you think about it, you could list out the folders as links which take the users to a normal list view for that folder, or many other things to help with navigation to and around the list which might be more helpful that the default “click on the folder and get lost” navigation you get in regular list views.

Similar Posts

15 Comments

  1. Why aren’t you filtering the items in the query itself?
    If the library has 2 folders and 10000 files, you’ll get a huge response that you don’t need, no?

    1. Itay:

      That’s absolutely true. I intended this as more of an example of something you might do with SPServices. There are several things I would probably change if I were to use this to build real functionality. (For instance, I create the var but use it only once.) It’s unlikely that you’d use theis code “as is” for anything.

      M.

  2. Nitpicker: I should be possible to add a CAML WHERE clause to only return folder items shouldn’t it? This way you would not unneccesarry iterate over all the content-items, especially in large lists.

  3. Thanks marc, just what I was looking for! Going to go and see how to get subfolders and follow it up the food chain!

    S’

  4. Thanks Marc for posting this. But I have some items present inside those folders. How can I list the items from the folders ?

    Thanks

  5. If you look at the original question that lead Marc to write this article, they asked how to pull in a list of documents organized by folder. I’ve got it working if anyone wants the code. It generated a UL of folder names and places the documents as LI items under the appropriate folder.

  6. There is probably an obvious reason for this, but without spending time in digging out the reason, just wanted to share this here, in case someone else is having trouble with this.

    In the example code (thanks for that)

    .find(“[nodeName=’z:row’]”).

    doesn’t seem to work for me, but replacing it with

    .SPFilterNode(“z:row”).

    does the trick.

    1. Yes, there was a change in jQuery 1.7+ that broke the .find("[nodeName='z:row']") syntax. That’s why I created the SPFilterNode function. Read more here.

      M.

Leave a Reply to Jussi Palo Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.