How to Add a Lookup Column in CAML for sharepoint 2010/sharepoint2007

I was creating a list definition in code which needed to lookup to a column of another list on the same site. After consulting this article, it seemed easy. Well as with many MSDN articles, they don't give the full picture. After playing with it for a while i finally came to a solution. Here are some gotchas:

Problem: The get information from lookup list was empty
Solution:
Here is my final XML for the field:

01
02
<Field ID="{3F55B8CF-3537-4488-B250-02914EE6C5A1}" 
03
Name="BoardsID" DisplayName="BoardsID" StaticName="BoardsID" 
04
FillInChoice="TRUE" List="Lists/Board" Type="Lookup"
05
Group="Custom Fields"
06
Required="TRUE" 
07
ShowInNewForm="TRUE" 
08
ShowInEditForm="TRUE" 
09
ShowInViewForms="TRUE">
10
      </Field>

Make sure firstly the "List" property has the correct url. Lists/[List Name]. Also make sure it contains nothing else, like "ShowField" property. It stopped working for me.

Problem: Field not showing on NewForm etc
Solution:

1. Check content types. My List in which the lookup field resided was inherting from the "item" content type. I needed to remove this. Search for the ContentTypes tag and remove all items:

1
<ContentTypes>      
2
    </ContentTypes>