Many times we have a need of creating a Lookupfield at the time of List creation. Many ways and many tips are there to create a Lookup field programmatically, but I am not focusing on how to create a lookupfield rather how to add an additional field option to an existing Lookup field / Item.
Assuming we have a site, within which we have 2 lists, “EmployeeList” and “DepartmentList”. The “DeptID” (from “DepartmentList”) is used as a lookup column within the “EmployeeList”. The steps below describes how to map one more additional field from the “DepartmentList” onto the “EmployeeList” based on the mapped lookupfield
"DeptID". SPFieldLookup spPrimaryField = ((SPFieldLookup)listEmployee.Fields["DeptID"]); SPFieldLookup field = (SPFieldLookup) listEmployee .Fields.CreateNewField (SPFieldType.Lookup.ToString(), "DeptLocation"); field.LookupList = spPrimaryField.LookupList; field.LookupField = "Location";// a column name from Primary lookup list "DepartmentList". field.PrimaryFieldId = spPrimaryField.Id.ToString(); field.ReadOnlyField = false; field.Direction = spPrimaryField.Direction; SPAddFieldOptions op = SPAddFieldOptions.Default; //Add Additional Field to main list listEmployee.Fields.AddFieldAsXml(field.SchemaXml, false, op);