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?
Asked
Active
Viewed 4.1k times
27
Stu Pegg
- 4,623
- 7
- 48
- 91
Alexey Krasheninnikov
- 2,750
- 2
- 24
- 42
1 Answers
43
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
Stu Pegg
- 4,623
- 7
- 48
- 91
Jaap Vossers
- 5,040
- 2
- 24
- 28
-
or .ToString() and yes check for null always – Anders Rask Feb 06 '10 at 10:29
-
Cool, at least this way we are protected from cases when hash/pound-sign(#) appears in the value. Thanks. – Alexey Krasheninnikov Feb 07 '10 at 20:19
-
12cool - just googled for this question and stumbled upon my own answer :) – Jaap Vossers Aug 12 '10 at 11:43
-
3I wrote an article about the whole get/set Lookup/User field method, which may be useful: http://tsstsst.com/easily-accessing-lookup-and-user-fields/ – Stu Pegg Jul 26 '12 at 11:38
-
1for CSOM, use FieldLookupValue from Microsoft.SharePoint.Client. – YHTAN Jul 25 '17 at 07:09