Friday, June 15, 2012

sharepoint get lookup value programmatically

Here is the C# code to get the Lookup field value :
SPList list = web.Lists["Tasks"];

                            SPListItem existingBranch = list.Items[0];

                            SPFieldLookupValue group = new SPFieldLookupValue(existingBranch["Food Coupons"].ToString());

                            string lookedUpItemTitle = group.LookupValue;
Here is the C# code to Set the Lookup field value :
 SPList list = web.Lists["Tasks"];

                            SPListItem newBranch = list.Items.Add();

                            newBranch["Title"] = "New lookup";

                            SPFieldLookupValue newValue = new SPFieldLookupValue(14, "Test");

                            newBranch["Sample"] = newValue;

                            newBranch.Update();

1 comment: