SharePointAds TextOnly

Thursday, 27 August 2009

Sharepoint: Programatically add LookUp field to custom list

We can add programmatically other 'LookUp' field as a new column into custom list, simply add below code.. :)

SPList targetList = web.Lists["List1"];
list.Fields.AddLookup("Col2", targetList.ID, false);
SPFieldLookup lookupField = list.Fields["Col2"] as SPFieldLookup;
lookupField.LookupField = targetList.Fields[SPBuiltInFieldId.Title].InternalName;

Suppose we have two different list List1 and List2, and in List1 have three columns as col1, col2, col3.
So with above code, it creates new lookup field as 'Col2' column in List2.


No comments:

Post a Comment