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());

 




2 Comments »

  1. What is myGood in multilookup.
    Is is a list or an array.

    Comment by kishan — December 8, 2014 @ 11:43 am

  2. “myGood.vendor” is a string variable. As a result it is something like this: “nike, asics, reebok, adidas”

    Comment by admin — December 8, 2014 @ 7:29 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment





MarkiMarta.com. Notes of web-specialist
Since 2009
18+