12 March 2009

Get Listitems from within folders in a SharePoint List recursively using SPQuery

Usually, a list defined in SharePoint consists of listitems that are stored within folders. One requirement while searching for data using SPQuery object is to get all listitems that match the specified criteria irrespective of where the data is stored in the folder hierarchy of the list.

Consider a list named “Students” that holds records of all students studying at a university. The records are stored within different folders. To get a list of students whose FirstName is “David”, the query would be

SPQuery query = new SPQuery();
query.ViewAttributes = "Scope="Recursive"";
query.Query = "<Where><Eq><FieldRef Name='FirstName'/><Value

The important thing to note here would be the use of the ViewAttributes property of the SPQuery object. The value of ViewAttribues property mentioned above instructs the SPQuery object to search recursively within all folders in the list.

No comments:

Post a Comment