views:

24

answers:

1

I'm trying to get the value of a list field using the SharePoint object model. Problem is that what should be the value is coming back as the field name. Code snippet below. The value is coming back as "City" instead of the actual city name. I know the value is not "City" because I checked it in the SPListItem Xml property. I have tried both the display name and the internal name as the key. I also tried SPField.GetFieldValue, but same result. What the heck is going on?


SPListItemCollection items = list.GetItems(query);

foreach (SPListItem item in items)
{
    SPField itemField;
    itemField = item.Fields["City"].ToString();   // returns "City" (!?!?)
}

+1  A: 

Try:

item["City"]

Your code is grabbing a reference to the City Field itself, not the value of the field for that particular SPListItem.

zincorp
Wow, I can't believe how non-intuitive that is. In any case, you are correct. Thank you!
anonymoose
It is intuitive, when you get used to SharePoint object model. :-)
naivists