How to retrieve and set lookup value in SharePoint2010

In this post, I’d like to show you how to retrieve and set the lookup value. In SharePoint2010, the lookup value you retrieve is in the format as {LookupItemID};#{LookupFieldValue}

You can download a solution here, after you deploy the SharePoint solution then you can run the console application as follows and the result is as follow:

Now, let’s look at the code to retrieve the lookup value, you can see I first retrieve the value by using the normal way of retrieving the lookup value and the value you got is 1;#Accounting. To parse the value, you need to create a SPFieldLookupValue type and you can grab the id using LookupId property and grab the value using LookupValue property.

To Set the LookupValue , you can create a SPFieldLookupValue type and assign to the lookup item or just assign the ID directly to the lookup item.