Working with lookup values in SharePoint Lists
Working with SharePoint lists I often use lookup values. But when I write C# solutions for lists I have some difficulties with LookUp Values. Trying to get LookUp Value as string it looks like "213;#lookup value".
At first I tried to use regular expressions. But some later I knew about commands for working with lookup list values. So here they are.
//first - initialize SPFieldLookupValue object for the Lookup property SPFieldLookupValue nsLookUpValue = new SPFieldLookupValue( ListItem["ColumnName"].ToString() ); //get the text value of the LookupValue from "213;#lookup value" string… String lookupText = nsLookUpValue.LookupValue;
And you don’t need neither RegEx, nor Pattern or any other things. Things are simple.
Update:
If you have multiple Lookup Values, you should use SPFieldLookupValueCollection
For example:
SPFieldLookupValueCollection vendors = item["VendorIds"] as SPFieldLookupValueCollection; List<string> myVendors = new List<string>(); foreach(SPFieldLookupValue vnd in vendors) { myVendors.Add(vnd.LookupValue); } myGood.vendor = string.Join(", ", myVendors.ToArray());