Monday, April 09, 2007

Defining a Lookup field in the schema is impossible?

I have been trying to get around a problem that has been troubling me with the Nested Tasks design.

You see, I wanted to put the whole list configuration into a list definition, which is basically an xml file which is easy to maintain. However, since we have a need for a lookup field, it looks like we are going to need to write code (a feature event receiver) to add that field, instead of having it in the list definition.


To define a lookup field, you need to specify at least two things:

  1. The GUID of the list its connecting to
  2. The name of the field in the list it connects to
  3. You may also specify (optional) the type of join and which field to display, so you can potentially connect to the ID field, but display the title field.

My problem is with item number 1 above – we don't have the GUID of the list, and there seems to be no way in the XML to simple tell it "I want you to connect to this list!".

When I was writing the design I was aware of this issue, but I was hoping research will uncover a way to accomplish that. I thought "there must be a built in feature that defines a lookup!", but I was wrong.

So unless any one of my readers knows how to define a lookup field in a list's schema, I will start working on a feature event receiver that will add the field to the list.

Come to think of it – this may actually be better, because now we can create a feature that just changes the existing lists, and not create a new list.

What do you think?


Anonymous said...

Hi. I've ran into the same problem with contenttypes and site columns, where a lookup site column defined in a feature needs the guid of the new list (which is also created in a feature).
Right now, I manually have to change the GUID in the site column feature to the new list...

Anonymous said...

Look at the Schema XML for the Blog Posts and Comments list - they provision a lookup field.

Chris O'Brien said...


I found myself in the same situation. Have a look at my 2 posts on this - the technique I use is to use CAML schema for the other properties but retrieve the list GUID in a feature receiver.

The first post outlines the problem/approach -

The second has the sample code -



Lee Dale said...

Theres a simple answer to this, instead of specifying the GUID of the list (which you dont know) you can simply specify Lists/your list name

e.g List="Lists/Articles"

This URL is taken from the Url attribute in the List node within the schema.xml where you define your custom list.