List all folders in a SharePoint list recursively

Can I get a list of all folders in a SharePoint document library using SPQuery?

Something that you might get on the filesystem if you opened a Windows command prompt and ran

 dir /b /A:D /S

      

The problem is that if you create a simple SPQuery and set the viewAttributes to Scope='RecursiveAll'

, the result set contains items, but not folders.

Or am I missing something completely?

Update: The reason for this question is that I have to create a solution where "package of files" makes sense.
When my client speaks of "document", it actually means an object that can be made up of multiple files and a common set of metadata for those files.

For example, the document might be called "Letter to my grandmother" (attributes: grandmother's address, letter title), but it consists of several files: an actual letter in an MS Word document and a JPEG image.

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

Since we expect that there will be many such documents, we also create a folder hierarchy with the usual folder type.

Now we come to the question: how do I show the client as "recent documents"? This should work recursively to list all "documents". Recursion through objects is SPFolder

too slow due to the number of requests that need to be executed. We were hoping for a recursive one SPQuery

, but it doesn't seem to be resolvable.

+2


a source to share


1 answer


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



<Eq><FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value></Eq>

      

+1


a source







All Articles