Include Items from Folders within your CAML Query Results
Sometime in our list on sharepoint we have a sub folder and we want to query only that folder.
This is a sample code you can apply to query :
SPSite site = new SPSite("http://mywss");
if (site != null)
{
SPWeb web = site.OpenWeb();
foreach (SPList docLib in web.Lists)
{
if (docLib.Title == "Shared Documents")
{
foreach (SPFolder subFolder in docLib.RootFolder.SubFolders)
{
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='Title'/></OrderBy>";
query.Folder = subFolder;
query.ViewAttributes = "Scope=\"Recursive\"";
System.Data.DataTable table = docLib.GetItems(query).GetDataTable();
}
}
}
}