How To Get The Value Of A Lookup Field Column In SharePoint



TODO:

Have you ever had a lookup column, and you want to get its value programatically using C#?

1.  I have a column that is called, Task Id.  

2.  "Task Id" in #1, is a lookup to the "ID" column of a list called "Tasks"

3.  I have an event receiver that needs to get the "Task Id" from the form.

 

SOLUTION:

 

//my event receiver code....
public override void ItemAdded(SPItemEventProperties properties)
{
     SPFieldLookup lookupField = properties.ListItem.Fields["Task ID"] as SPFieldLookup;
     SPFieldLookupValue taskIdVal = null;

     object obj = properties.ListItem["Task ID"];
     if (lookupField != null)
          taskIdVal = lookupField.GetFieldValue(obj.ToString()) as SPFieldLookupValue;

     string taskId = taskIdVal.LookupValue;
}

 

 

NOTES:

There are no notes on this topic.



Comments (2) -

Jussi Palo
Jussi Palo
8/7/2014 8:54:14 AM #

Thanks! Small correction, though, line 9 should say "taskIdVal = lookupField..." instead of "taskIdVal = lookField..."

Donnie Wishard
Donnie Wishard
8/7/2014 9:00:31 AM #

Thanks for catching that.  Post updated to reflect your fix.  Much appreciated!

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading