Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I have created several lookups in the schema.xml without problems. The declaration uses the List attribute as you have in your example though I have never used the SourceID attribute. The only issue that has to be met is the order with which the lists are created. For example when I did this, I had to be sure that the list that was being looked into was created first. This has always worked for me. There is nothing wrong with using code either so it really depends on what you want to do.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.