Creating Lookup Columns using CAML
As List IDs are generated at runtime and are non-deterministic, it is not possible to refer to the target list using List Id. So, as a work around, most of the solutions on the internet suggest to write Custom Code. But this can be avoided and the lookup columns can be created using CAML as shown below:
While creating the lookup columns using CAML, the target list needs to be referred using the Url of the List Name instead of the List Id.
Note: Please note that the entire URL of the List need to be specified i.e., Lists/Products instead of just the list name
Also, the target list needs to be present in the folder, when this column is getting created.
<Field Type="Lookup" DisplayName="Language" Required="TRUE" List="Lists/Products" ShowField="Title" UnlimitedLengthInDocumentLibrary="FALSE" ID="{52c458b8-5f67-428c-a675-02a0538e7c84}" SourceID="{8ea5965e-0026-48c7-b426-a8bca5b4f543}" StaticName="Language" Name="Language" ColName="int1" RowOrdinal="0" Group="" Version="1" />
Self referential Lookup Column:
For scenarios where you wanted to maintain a hierarchy among the items in the same list, you might want to refer a column in the same list.
But you cannot use GUID of the list, as the list is not yet created by then. In such scenarios, the same list can be referred by using the keyword “Self” as the List Name.
<Field Type="Lookup" DisplayName="Parent" Required="FALSE" List="Self" ShowField="Title" UnlimitedLengthInDocumentLibrary="FALSE" ID="{d2a9d2e6-9beb-414d-bba6-fc0b3fa7e494}" SourceID="{a26e88fb-94b6-4429-8d16-da52faa0250c}" StaticName="Parent" Name="Parent" ColName="int1" RowOrdinal="0" />
Comments
Anonymous
August 14, 2009
Hi Hari, I was able to create a site column using the List="List/ListName" option and the site column got created just fine how ever when I add the site column to a list it throws up a Exception from HRESULT: 0x80040E07 the ULS logs were not of much help can you let me know if I am missing some thing hereAnonymous
May 09, 2010
I had the same problem as vejay could someone help with this plz