Everyone is close, but not quite right.
using (SPSite site = new SPSite("http://server/site"))
{
SPWeb web = site.RootWeb; // See disposal guidance http://blogs.msdn.com/b/rogerla/archive/2008/10/04/updated-spsite-rootweb-dispose-guidance.aspx
SPQuery query = new SPQuery();
query.Query = @"
<Where>
<BeginsWith>
<FieldRef Name='ContentTypeId' />
<Value Type='ContentTypeId'>0x0120</Value>
</BeginsWith>
</Where>";
query.ViewAttributes = "Scope='RecursiveAll'";
SPList list = web.Lists[listId];
SPListItemCollection items = list.GetItems(query);
// Do stuff with your folders
}
First of all, using this FieldRef is wrong:
<FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value>
because the folder content type can be inherited. Therefore, you need to compare against the content type ID, like this:
<Where>
<BeginsWith>
<FieldRef Name='ContentTypeId' />
<Value Type='ContentTypeId'>0x0120</Value>
</BeginsWith>
</Where>
And then, set the view attribute Scope to RecursiveAll
<View Scope='RecursiveAll'>...</View>
That should return any item whose content type inherits from Folder (0x0120)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…