SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom page that copies items from one list in one site to another, and its failing with the exception above.

I tried checking the field using sharepoint manager but I didnt find anything strange. The field where its failing its a managed metadata column the strange thing its that the exception is shown when checking if the field[internalname] is != null

  foreach (SPListItem item in listItems)
                                    SPListItem targetItem = destinationList.Items.Add();
                                    foreach (SPField field in item.Fields)
                                        if (!field.ReadOnlyField && field.InternalName !="Attachments")
                                            if (item[field.InternalName] != null)

Update 1:

I tried these 2 ways and I still have the same error, its a managedmetadata field so no idea why its happeningn

   //if (item.GetFormattedValue(field.InternalName) != null)               
                                        if (item[field.Id] != null)
share|improve this question

You should rather try using item.Fields.ContainsField ( or any of the overloads to check whether indeed that particular field exists in your destination list.

share|improve this answer
I dont understand how is it possible that it doesnt exist in the list if I am iterating over the SPField of that list? – Esteban V Sep 24 '12 at 17:10
one potential answer could be valid when iterators are not supporting the iterations by that value - it all depends on the Iterator implementation. – Marius Constantinescu - MVP Sep 24 '12 at 18:48
up vote 2 down vote accepted

Solution(s): It seems there can be several causes. 1). Use of invalid field name. You need to use the internal field name! However, that was not the case in my situation. 2). Change the List View Lookup Threshold value of the web application. Default value is 8, I changed it to 20 and big surprise: it worked!

share|improve this answer
Thanks Luis!, this worked for me. What a weird problem this was. Weird thing was that I had no problem on the application server (backend), but the problem only occurred on the two WEB Front end servers. Also in a console app on the web front end server I could get the values and also with SharePoint Manager, just not through the portal. Maybe on the app server the limitation doesn't count? I tested with the same account on all servers and also had my code with elevatedpriviliges. – user11282 Oct 11 '12 at 14:07

Have you debugged your code properly?

to me an error like:

Value does not fall within the expected range when iterating over all fields

means that [] is set to an index and you could possibly passing text instead?

so looking at your code:

if (item[field.InternalName] != null) 

the item[] I would presume should hold a int value and not text but your putting in field.InternalName what is text am i not wrong?

The best way to find out is to pub a break point at that location and attach to process. then hover over item and go through and see what it contains! Another way is in code delete from item[field.InternalName] so it looks like item and then add [ so it looks like item[ then see what it says in the yellow box and do you have an option to choose from int index or string? also the error indicates that is not even checking up on the != null as its breaking on item[field.InternalName].

so you need to make sure if its setup to take an int value or string value. If it is an int value and you are passing an int value than the indexing is wrong.

can you try this instead?

if (item.GetFormattedValue(field.InternalName) != null)               

A string that contains either the internal name or the display name of the field.

hope this helps :)

share|improve this answer
cool answer, its actually weird that code was developed by someone else and it was working and suddenly its giving that exception, about the indexers, I dont really understand, if I am iterating over the SPFieldsCollection of the SPList in the first place then all fields should have an internal name that is a string not an int.? sharepoint bug? or misunderstanding from myself of how fields were developed by msft – Esteban V Sep 25 '12 at 11:05
thats cool, i think its more of a miss understanding ;) , i too get confused sometimes. usually [] indecates its an interger value but you can sometimes have string values if the option is available. But for your case i think GetFormattedValue is better as you wanted to check on the internal name :). I hope my above answer has worked out for you :) – ali Sharepoint Sep 25 '12 at 15:22

It seems odd as you get the field for the items own collection of fields.

But as you has the SPField I'd anyway recommend using field.Id instead of field.InternalName

share|improve this answer
pls see update1, I tried both and it didnt work – Esteban V Sep 27 '12 at 9:58

I understand this was answered over two years ago but I found a better fix for this other than increasing the List View Lookup Threshold.

As recommended here.

You should not increase the list threshold above 8/12 if you can help it due to the performance issues that can occur.

Stumbling around the internet I found three articles offering solutions to this problem that didn't require a threshold change:

So if you change your code to look something like below the error should go away without requiring an increase in the List View Lookup Threshold.

foreach (SPListItem itemLookup in listItems)
    SPListItem item = listItems.GetItemById(itemLookup.ID);
    SPListItem targetItem = destinationList.Items.Add();
    foreach (SPField field in item.Fields)
        if (!field.ReadOnlyField && field.InternalName != "Attachments")
            if (item[field.InternalName] != null)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.