3

Is it possible to get a list of all folders in a SharePoint document library, using SPQuery?

Something you could get in file system, if you opened Windows command prompt and ran

 dir /b /A:D /S

The problem is, if you create a simple SPQuery and set the viewAttributes to Scope='RecursiveAll', the result set contains items, but not the folders.

Or am I completely missing something?

Upd.: The reason for this question is (was) that I have to create a solution where a "packet of files" makes sense.
When my client speaks about a "document", he actually means an entity that may consist of multiple files and a common set of metadata for these files.

For instance, the document might be named "A letter to my grandma" (attributes: grandma's address, letter title), but it consists of several files: the actual letter in MS Word document and a JPEG picture.

So, the idea was that I could create a content type derived from Folder and add some fields to this content type (address, title). All the files placed into that folder would naturally become parts of the 'document'.

Since we expect that there will be a lot of such documents, we create a folder hierarchy of regular type of folders, too.

Now, we come to the question: how do I show a view like "Recent documents" to my client? This must work recursively to enumerate all the "documents". Recursion through SPFolder objects is way too slow due to the number of requests that have to be made. We hoped for a recursive SPQuery, but this does not seem to be solvable this way.

naivists
  • 32,681
  • 5
  • 61
  • 85
  • Dupe of http://stackoverflow.com/questions/1557675/caml-query-that-includes-folders-in-result-set Unfortunately not clear in that post if its been solved or not – Ryan May 20 '10 at 10:00
  • Is there any particular reason you're trying to query all of the folders instead of iterating through SPList.Folders? I'm doing some query tests now, but I'm curious about this. – Grace Note May 20 '10 at 13:58
  • @ccornet, yes I don't want to do it in an iterative manner. Updated my post to show the details. – naivists May 20 '10 at 19:25
  • @naivists: Why don't you just create a new list/content type which keeps track of the recent documents and show that? Will be much faster. –  May 21 '10 at 23:20
  • @Moron been there, done that, but hoped for something better – naivists May 22 '10 at 16:07
  • @naivists: better in what sense? Space usage? Time to display the recent documents to the user? What about using CreationDate/ add your own date column to each doc (will not waste space) to track the most recent ones? –  May 22 '10 at 18:58

1 Answers1

1

You should be able to get all the folders by filtering on the content type.

<Eq><FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value></Eq>
Rob Windsor
  • 6,744
  • 1
  • 21
  • 26
  • This works for the items in the folder you are currently in. I have a folder hierarchy in which I have to pick out only _some_ specific folders (being of different content type). See my post update for details. – naivists May 20 '10 at 19:26