Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint ListItem Error: "Value does not fall within the expected range"

Hi I am developing using the SharePoint namespace and I ran into the following error when I try to retrieve a Title field from the list items.

Value does not fall within the expected range

I know however that the field exists because I printed out all the fields.

string value = (string)listItem[listItem.Fields["Title"].Id];
Console.WriteLine("Title = " + value);

Update: To what extent does the View that was used to retrieve the list items play a role in what fields will be available? This code fails with the same exception:

SPListItemCollection items = list.GetItems(list.DefaultView);
foreach (SPListItem listItem in items)
{
  try
  {
    Console.WriteLine("Title = " + listItem.Title);
  }
  catch (Exception e) 
  { 
    Console.WriteLine("Exception: " + e.Message); 
  }
}

In both cases the list.DefaultView property was used to retrieve the list items.

like image 902
Ries Avatar asked Mar 18 '09 07:03

Ries


4 Answers

I don't know if your error is solved or not. But I was facing the same issue and I found the solution to the problem.

You have to go to:
Central Administration > Application Management > Manage Web Applications
select the Web Application in use and choose:
“General Settings > Resource Throttling” via the Ribbon.

In that scroll down and look for "List View Lookup Threshold" in that by default value is 8 increase the value till the error is gone.

Reference Link

like image 192
Rahul Gokani Avatar answered Nov 13 '22 14:11

Rahul Gokani


The "Title" field may exist in the list but not in the default view. Can you do this?

foreach (var item in list.Items) Console.WriteLine((string)item["Title"]);
like image 43
vitule Avatar answered Nov 13 '22 14:11

vitule


I had this issue because the column was not part of the default view. Instead of relying on the view, query for the specific columns directly when creating the SPListItemCollection.

  SPList cityList = web.Lists[cityListName];
  SPListItemCollection items = cityList.GetItems("Title","LocCode");
  foreach (SPListItem item in items)
     {
          cityName = item["Title"].ToString().Trim();
          cityCode = item["LocCode"].ToString().Trim();
     }
like image 2
Ernest Correale Avatar answered Nov 13 '22 13:11

Ernest Correale


I'd suggest something like

if (item.Fields.ContainsField("Last_x0020_Modified"))
{
    if (query.ViewFields.Contains("Last_x0020_Modified"))
    ...

B/c if the field wasn't requested in the SPQuery.ViewFields, you probably can't get it (exceptions include Created field, ID field)

like image 1
Perry33 Avatar answered Nov 13 '22 13:11

Perry33