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 am getting the error on the following line for a timer job:

if (listItem["Department"] != null)

Normally this error indicates that it cannot find the field, but there are no spaces and the field label name and strong name are the same. Furthermore, if I write it like this:

if (listItem.Fields["Department"] != null)

I no longer get the error.

The problem is I am trying to call TaxonomyFieldValueCollection on the object and cannot call it by Fields. Is there some nuanced difference between the two that I'm missing that's causing this error?

EDIT: I figured out the problem. I had set query.ViewFieldsOnly = true; and "Department" was a new field and was not set in the ViewFields, so as far as the timer job could tell the field didn't exist. I simply needed to add the field into the list of fields in ViewField.

share|improve this question
    
If you've resolved this, please post your solution as an answer for future visitors. –  RJ Cuthbertson Feb 10 '13 at 16:06
    
I tried to do this before but was blocked from posting an answer. I can do it now it seems. –  user11457 Feb 12 '13 at 18:58
add comment

2 Answers 2

up vote 2 down vote accepted

I figured out the problem. I had set query.ViewFieldsOnly = true; and "Department" was a new field and was not set in the ViewFields, so as far as the timer job could tell the field didn't exist. I simply needed to add the field into the list of fields in ViewField.

share|improve this answer
    
What do you mean by in ViewField? –  Shane Courtrille Jul 4 '13 at 18:05
add comment

The first line checks if the value of the data in the column "Department" is null for that specific list item.

The second line checks if the SPField object "Department" is null.

If the SPField exists on the list, it should never be null, but the data stored in that field for a specific list item may be.

share|improve this answer
    
It's not that the line is resolving incorrectly, it's that there is an exception and gives the "value does not fall within the expected range" error. Please ignore the "if" statement, I am talking purely about the behavior of getting the value of the list item. The SPField line was just an example to show that the value can be pulled with the name "Department". –  user11457 Jan 28 '13 at 16:53
    
Does the field happen to have a different internal field name? –  RJ Cuthbertson Jan 28 '13 at 18:08
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.