I am developing a solution which has multiple OOTB lists like Links, Contacts, Custom List and Document Library.
And, I have to create a lookup field in four lists which refer to a field in a Custom Library.
Online, I saw many people having difficulty while setting the lookup field declaratively in the schema.xml file of the list. like:
Field Type="Lookup"
DisplayName="Test field"
Required="FALSE"
List="Lists/Tasks"
ShowField="Title"
UnlimitedLengthInDocumentLibrary="FALSE"
ID="{95d89725-eb97-428b-bc79-ee02ca8b7225}"
SourceID="http://schemas.microsoft.com/sharepoint/v3"
StaticName="TestField"
Name="TestField" />
But, as the GUID to the list is assigned on instance creation, they were having problems.
Now, I am creating the Lookups for the list through code on an event, like:
newList.Fields.AddLookup("Lookup Column", targetList.ID, false);
SPFieldLookup lkp = (SPFieldLookup)newList.Fields["Lookup Column"];
lkp.LookupField = targetList.Fields["Title"].InternalName;
lkp.Update();
Is this OK or is there a better way around this?