3

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)

5 Answers 5

2

You should rather try using item.Fields.ContainsField (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldcollection.containsfield.aspx) or any of the overloads to check whether indeed that particular field exists in your destination list.

2
  • I dont understand how is it possible that it doesnt exist in the list if I am iterating over the SPField of that list? Commented Sep 24, 2012 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. Commented Sep 24, 2012 at 18:48
2

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!

http://blog.vanmeeuwen-online.nl/2012/07/value-does-not-fall-within-expected.html

1
  • 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
    Commented Oct 11, 2012 at 14:07
1

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.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.getformattedvalue.aspx

hope this helps :)

2
  • 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 Commented Sep 25, 2012 at 11:05
  • 1
    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 Jafer
    Commented Sep 25, 2012 at 15:22
1

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

1
  • pls see update1, I tried both and it didnt work Commented Sep 27, 2012 at 9:58
1

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:

http://ehikioya.com/lookup-column-error/

http://joshihemant.wordpress.com/2012/05/29/error-value-does-not-fall-within-the-expected-range/

http://ranaictiu-technicalblog.blogspot.com/2010/12/sharepoint-2010-error-list-view-lookup.html

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)
            {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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