We've updated our theme! Please refresh your browser if things don't look right.
vote up 2 vote down
star
1

When you get an SPListItemCollection from a SPList.GetItems(SPQuery) request, and the items have SPFieldUser or SPFieldLookup fields, what is the sexy way to get typed values, not the item["Field"].ToString().Split('#')[1] way, please?

flag

1 Answer

vote up 5 vote down
check
new SPFieldLookupValue(item["FieldName"] as String).LookupValue

you probably want to check if item["FieldName"] is not null or empty before doing this.

SPFieldUserValue works similarly, but you need to pass in an SPWeb into the constructor, which you can retrieve from your SPListItem using SPListItem.ParentList.ParentWeb

link|flag
or .ToString() and yes check for null always – Anders Rask Feb 6 at 10:29
Cool, at least this way we are protected from cases when hash/pound-sign(#) appears in the value. Thanks. – AKrasheninnikov Feb 7 at 20:19

Your Answer

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