Skip to main content

Share Point

Go Search
Home
Announcements
Who Are We
Blogs
  
Share Point > Tips & Tricks > Querying Sub Folders with SPQuery Objects  

Web Part Page Title Bar image
Querying Sub Folders with SPQuery Objects

With SPQuery class, we can search lists just like views.
 
Thik of a document library with lots of documents and sub folders. To get the filtered list of items in this document library, your code will be similar to the one below:
 

SPQuery pagesQuery = new SPQuery();

pagesQuery.Query = "<Where><Eq><FieldRef Name=\"FileID\" /><Value Type=\"Text\">" + FileID + "</Value></Eq></Where>";

pageUrl = currentWeb.Lists[pagesList].GetItems(pagesQuery)[0][pagesFileRefFieldName].ToString();

If you run this code, you'll see that the results of this query will only be from the root folder of the document library. If you want the result set to be from all items in the document library regardless of the folder the item in, you have to add this line of code for the SPQuery object below before executing the query:

pagesQuery.ViewAttributes = "Scope=\"Recursive\"";