Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

4 Answers

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.

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? –  L.V. Sharepoint Architect 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. –  C. Marius - MVP Sep 24 '12 at 18:48
add comment

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

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 –  L.V. Sharepoint Architect Sep 25 '12 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 Sharepoint Sep 25 '12 at 15:22
add comment

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 –  L.V. Sharepoint Architect Sep 27 '12 at 9:58
add comment
up vote 1 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!

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

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
add comment

Your Answer

 
discard

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.