I have a couple of utility functions that help with querying lists and wanted to see if they I wrote them would cause either memory leaks or cause the client code to try to access already disposed objects.
public static SPListItemCollection GetListItems(string RelativeURL)
{
SPList list;
int length = RelativeURL.LastIndexOf("/", StringComparison.OrdinalIgnoreCase);
string webUrl = RelativeURL.Substring(0, length);
string listName = RelativeURL.Substring(length + 1);
using (SPWeb web = SPContext.Current.Site.OpenWeb(webUrl))
{
list = web.Lists[listName];
}
return list.Items;
}
public static SPListItemCollection GetListItems(string RelativeURL, string CamlQuery)
{
SPQuery query = new SPQuery();
query = new SPQuery();
query.Query = CamlQuery;
return GetListItems(RelativeURL, query);
}
The client code would look like this:
SPListItemCollection items = GetListItems("/subsite/mylist");
or
SPListItemCollection items = GetListItems("/subsite/mylist", "<where>...");