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

8

7

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.

Besotted answered 18/3, 2009 at 7:27 Comment(0)
P
2

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();
     }
Pyuria answered 26/3, 2016 at 13:34 Comment(0)
U
5

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

Unguentum answered 17/2, 2014 at 10:1 Comment(1)
I had this very problem. I had validated that my field names were correct. Also, it appears Microsoft has changed the default value of this from 8 to 12. I had 12 in my development environment but 8 in the staging environment where the problem arose. I had to reset the app pool in IIS for the setting change to take effect.Intemperate
C
2

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)

Considering answered 13/4, 2011 at 19:7 Comment(0)
P
2

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();
     }
Pyuria answered 26/3, 2016 at 13:34 Comment(0)
C
1

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"]);
Crust answered 18/3, 2009 at 15:4 Comment(2)
Any attempt to reference item["Title"] throws that exception.Besotted
I am facing same problemAccretion
Y
0

I don't know why, but I created a GridView from SPListItemCollection as a DataSource. I had the same error. But I tried to get the dataTable from collection:

SPListItemCollection res = da.getAllItems();
DataTable dt = res.GetDataTable();

And then I see in my GridView that field that contains titles of list items is called "LinkTitle". So, I rewrited my code: SPListItemCollection res = da.getAllItems();

gridView.DataSource = res.Cast<SPListItem>().Select(a=> a["LinkTitle"] );
gridView.DataBind();

And everything works fine :)

Yoder answered 29/12, 2010 at 14:35 Comment(0)
P
0

The "Title" field is not (by default) available in the default view. If you have a look at the view settings page, Title is checked, but it is labeled as being "linked to item with edit menu".

The field exposed by the view is actually named "LinkTitle" (you can confirm this by enumerating list.DefaultView.ViewFields).

The solution to your problem is simply to replace item.Title with item["LinkTitle"].ToString().

Putter answered 8/3, 2011 at 1:13 Comment(0)
Q
0

This problem can occure if you rename an already existing Column. Try deleting it and create a new one with the same name.

Or in another case that occured to me, I had accidentally wrote a letter of the column name in Greek! eg. "ΙsCooperation" Ι was a greek character

Quechuan answered 29/10, 2020 at 15:6 Comment(0)
P
0

I was using a CamlQuery and was conditionally filtering based on certain code value to use <Eq> or <Contains> clause. Problem was, my condition was setting <true> or <false> which was incorrectly being calculated and hence it is not a valid comparison operator, it was throwing such error.

Solutions of course was to use correct operator for comparison.

Pendent answered 4/11, 2023 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.