CAML query that includes folders in result set

前端 未结 7 1885
孤街浪徒
孤街浪徒 2020-12-16 01:29

I\'m trying to write a CAML query that executes against a specific SPList, scoped to a specific folder, recursive from that point, and returns all ListItems (which meet a cr

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 02:23

    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 = @"
              
                
                  
                  0x0120
                
              ";
      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:

    Folder
    

    because the folder content type can be inherited. Therefore, you need to compare against the content type ID, like this:

    
      
        
        0x0120
      
    
    

    And then, set the view attribute Scope to RecursiveAll

    ...
    

    That should return any item whose content type inherits from Folder (0x0120)

提交回复
热议问题