web 2.0

Retrieve a List's LookupValue ListItem

When using a lookup value in a list column, we sometimes need the entire item that is being looked up, in addition to it's value. Here's how to do it:

SPSite spsite = new SPSite("http://SiteAddress")
SPWeb spWeb = spsite.OpenWeb();

// Remember to get the ListItem through your preffered method :)

// Use the  'SPFieldLookupValue' class to create an object......

SPFieldLookupValue lookupValue = new SPFieldLookupValue(listItem["Insert Column Name here"].ToString());

// Get the LookUp's List item's ID (which is of an integer datatype)

int itemID = lookupValue.LookupId;

// Get the LookupValue's List Item....
SPListItem lookupValueListItem = spWeb.Lists["ListName"].GetItemById(itemID);

// Get the value in the lookup.

// This might be of interest,

// because, using 'listItem["Insert Column Name here"].ToString()' gives us

// some unnecessary values like '#'.

string value = lookupValue.LookupValue;

 

So, now we have the LookupValue's list item in 'lookupValueListItem' and the lookup's value in 'value'.

 

Happy Sharepointing :)

Comments

Add comment


 

biuquote
  • Comment
  • Preview
Loading