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.
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.