How to define lookup column via CAML in SharePoint 2010 and avoid errors

As it turned out you can kill a day trying to define a lookup column correctly via CAML in SharePoint 2010. Especially when you do it for the first time!

Let’s start from scratch.

The simplest text column definition in CAML may look like the following:

<Field ID="{BF265F75-5F3A-430A-9024-3BF5F2A48244}" Name="SomeField" Type="Text" DisplayName="Some Field" Group="Custom Field" />

Here we define unique field ID, name, type, display name and group. All that parameters are self-descriptive and straightforward.

If we want to declare a lookup column we need to add some additional parameters like on the example below:

<Field ID="{66F964D2-181A-4EA4-B705-8012571A9DE5}" Name="LookupField" Type="Lookup" List="Lists\ValuesList" ShowField="Title" DisplayName="Lookup Field" Group="Custom Group" />

Here our field has type Lookup. List attribute defines list URL the field has to get values from. ShowField specifies a target field name to look up.

It works fine. The field is deployed and lookup works as expected… until you try to save your site as template. If you try this you get the error:

 Error exporting the site field named "Lookup Field"

Digging in the logs gets us the original error:

SPSolutionExporter: System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

Looks like our field ID is incorrect! Really? Ok. Let try to modify it and remove curly brackets from ID definition to meet the format:

<Field ID="66F964D2-181A-4EA4-B705-8012571A9DE5" Name="LookupField" Type="Lookup" />

After redeploying the solution export works fine. Great! But… now we can’t redeploy the solution from Visual Studio as it reports the error:

Error occurred in deployment step 'Activate Features': Invalid field name. {66f964d2-181a-4ea4-b705-8012571a9de5}

Visual Studio expects column ID be with curly brackets, but SharePoint doesn’t. That creates a lot of problems if leave it this way.

I’ve killed a day to find a solution to that problem. Lookup column definition requires overwrite attribute set to ‘TRUE’. If this value is set lookup columns are deployed and exported correctly. Otherwise we had errors above.

This way valid lookup field definition looks like:

<Field ID="{66F964D2-181A-4EA4-B705-8012571A9DE5}" Name="LookupField" Type="Lookup" List="Lists\ValuesList" Overwrite="TRUE" ShowField="Title" DisplayName="Lookup Field" Group="Custom Group" />

That is minimal set of fields required to make our lookup column work as expected, be deployed and exported without errors.

Advertisements
How to define lookup column via CAML in SharePoint 2010 and avoid errors

3 thoughts on “How to define lookup column via CAML in SharePoint 2010 and avoid errors

  1. Sal says:

    Andrew,

    Great post! Now I am able to save my site as a template. However, another problem appeared — my look-up fields now do not contain information anymore. I usually specify initial list data in “” sections in list instance Elements.xml. When Overwrite=”TRUE” attribute is used in field definition, such look-up fields are always empty after site collection is created. Do you know why it happens and what can be done?

    Sal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s