Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAML Query to select top level folders only

I want to select the list of folders (without subfolders, not recursive) of a Document library using CrossListQueryCache.

Everything is working fine except that i am receiving ALL the folders and subfolders in the list and not just the first level of folders. what do i need to change in the code below so that only the first level of folders is returned without their subfolders and sub-subfolders etc...

string query = string.Empty;
string websQuery = string.Format("<Webs Scope=\"{0}\"/>", "None");
string lists = "<Lists ServerTemplate=\"101\"" + " ><List ID=\"" + listid + "\" /></Lists>";  
bool useList = true;
string relativeUrl = this.GetRelativeUrl();
query = string.Format("<Where><Eq><FieldRef Name='FSObjType' /><Value Type='LookUp'>1</Value></Eq></Where>", relativeUrl); 

CrossListQueryInfo info = new CrossListQueryInfo();
info.Lists = lists;
info.Webs = websQuery;
info.Query = query;
info.ViewFields = "<FieldRef Name=\"FileLeafRef\"/>";
info.WebUrl = web.ServerRelativeUrl;
CrossListQueryCache cache = new CrossListQueryCache(info);
SiteDataResults sd = cache.GetSiteDataResults(site, true);
like image 656
Zee99 Avatar asked Nov 14 '22 19:11

Zee99


1 Answers

Use SPQuery.ViewAttributes with "Scope" different from "Recursive":
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.viewattributes.aspx

If you want to display only folders or NOT folders then you can add info about ContentType, like here (Drax's answer):
CAML queries: how to filter folders from result set?

like image 80
Marcin Robaszyński Avatar answered Dec 21 '22 13:12

Marcin Robaszyński