Mittwoch, 8. Februar 2012

value does not fall within the expected range -> Sharepoint list

I had the following code:
public static SPListItemCollection GetListItemCollection(string query)
{
SPListItemCollection listitemcollection = null;
SPList list = web.Lists["TestList"];
listitemcollection = list.GetItems(query);
return listitemcollection;
}


the issue was, that I did receive a SPListItemCollection object, but with no fileds in it, when debugging. When trying to access the field, the error displayed was:

I solved the problem in adding a SPQuery object to it. It seems, that the GetItems() method-overload, does not accept the query-string as a string overload. You need to pass it into the property of the SPQuery object and then overload with the object. This worked like a charm. Here the code:

public static SPListItemCollection GetListItemCollection(string query)
{
SPListItemCollection listitemcollection = null;
SPList list = web.Lists["TestList"];
SPQuery spquery = new SPQuery();
spquery.Query = query;
listitemcollection = list.GetItems(spquery);
return listitemcollection;
}


Keine Kommentare:

Kommentar veröffentlichen