Deploying Lookup Fields in a SharePoint Feature (part one: Lists)

Deploying a list that contains a Lookup field in SharePoint requires a very specific approach. Why are Lookup fields different from other fields? The core issue is that a Lookup field references a list instance’s GUID when the field is created. If you are also creating your list instance as part of the deployment, the list’s GUID is not available because it is not defined by the feature, but instead is assigned automatically during feature activation. Fortunately, there is an easy way to deal with this when you are defining your Lookup fields in a Schema.xml.

Assume that the following list instance is being created as part of your feature deployment:

If you want to use this as a target for a Lookup field in another list, you can’t refer to it by its GUID since you don’t declare the GUID in your instance definition. Instead you can refer to the target list by its path when you define the Lookup field in the new list’s Schema.xml, as shown below: 

 Later, when you create a list instance based on this schema, the lookup column will be correctly wired up to the LookupList list instance. One caveat: In your feature definition, you must include the source list’s instance definition (LookupList) before the consumer list definition (CustomLookupList). 

An interesting twist arises when you need to create a list which contains a lookup reference to itself. This is also handled quite simply by changing the List reference in the field definition to “Self”.
 

These simple methods allow you to cleanly deploy Lookup fields in the list schemas of your feature. Unfortunately, this technique does not work when deploying Lookup Site Columns.

Next time: Defining Lookup Site Columns in a feature

This entry was posted in Development, Development, SharePoint 2007, SharePoint 2010. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>