SPQuery Doesn’t Search Sub Folders

If you are using the SharePoint object model to query a SharePoint list you will find out that SPQuery doesn’t retrun items in the subfolders. In  order to execute a search query against a list and return results regardless of subfolder location you need to add “Scope=’RecursiveAll'” attribute to the SPQuery object’s ViewAttributes property.  Since the ViewAttributes property is a string you need to make sure that this attribute doesn’t already exist.  If it exists you must remove it and then add it back to the ViewAttributes property.   

 

SPListItemCollection

foreach

(SPListItem item in itemCollection){
     //do something
}
SPQuery query = new SPQuery();
query.ViewAttributes += ” Scope=’RecursiveAll'”;
itemCollection = list.GetItems(query);

Bookmark and Share

Leave a Comment

Your email address will not be published. Required fields are marked *