I'm looking for a way to search for folder which name starts of 'Foo' using caml query in list. How can I narrow results to return only folders?
1 Answer
you can filter on folder content type
try this
// Source data retriever
SPSiteDataQuery query = new SPSiteDataQuery();
// Table to hold retrieved information
DataTable dt = null;
// Set data retrieve settigns
query.Lists = "<Lists BaseType='1'/>";
query.Webs = "<Webs Scope=\"Recursive\" />";
query.ViewFields = "<FieldRef Name=\"" + Fields.Title + "\" />" + "" +
"<FieldRef Name=\"Name\" />" + "" +
"<FieldRef Name=\"Fileref\" />" + "" +
"\" />";
query.Query = CamlQuery;
dt = web.GetSiteData(query);
if (dt != null)
{
foreach (DataRow row in dt.Rows)
{
// Do your job
}
}
And CAML query:
<Where>
<And>
<BeginsWith>
<FieldRef Name='ContentType' />
<Value Type='Computed'>Folder</Value>
</BeginsWith>
<BeginsWith>
<FieldRef Name='Title' />
<Value Type='Text'>Foo</Value>
</BeginsWith>
</And>
</Where>
Here is a MSDN source for content types ids.
Hope it helps,
Andrew