I developed a custom WebPart which allowed a user to select a list from a site and then execute an advanced search within the selected list. The advanced search functionality provided various options to select which fields to filter on, sort and display, using the SPField properties.
The search execution code was created using the SPList.GetItems(SPQuery query) search function.
Testing was going great until i came across a problem when searching on items in subfolders within a list, the test failed as No items in subfolders were returned.
I came across this in the SDK which wasn’t obvious to me, so thought I'd ease other people’s pains quickly if it isn’t obvious to you either.
There is a property called ViewAttributes on the SPQuery object which accepts a string value. The following code shows how to use the property to search recursively through subfolders
- using (SPSite site = new SPSite("http://mysite.com/sitecollection"))
- {
- using (SPWeb web = site.OpenWeb("mysitename"))
- {
- SPList list = web.Lists["mylist"];
- SPQuery query = new SPQuery();
- query.ViewAttributes = "Scope=\"Recursive\"";
- query.Query = string.Format("<Where><Eq><FieldRef Name={0} /><Value Type='{1}'>{2}</Value></Eq></Where>", "myfieldname", "text", "searchstring");
- SPListItemCollection itemsReturnedFromQuery = list.GetItems(query);
- }
- }
0 comments:
Post a Comment