Fix Lookup Fields created through Features

When creating site columns through features you may have become stuck when it comes to Lookup columns. The Lookup field type is supported by the feature XML, however there is one catch. You must provide a unique GUID to the list that contains the lookup values. The problem is, we don’t know what the GUID to the lookup list is, as either we are creating it through features, or even if we get the GUID through the UI, it could easily change.

SharePoint 2010 has resolved this issue by allowing variables within the feature XML that will automatically add the correct list GUID when the feature is activated. However for SharePoint 2007 we need to provide a solution ourselves.

Step 1

Create your feature as you normally would and be sure to include a feature receiver. In the Field XML, add a unique GUID for the List attribute and make note of it.

<Field ID=”{1BB2D41A-EA27-42e4-AED7-EFF558F1DF21}”     DisplayName=”RegionType=”LookupName=”RegionShowField=”TitleList=”{B5D4DCEE-61EB-4c53-9A9A-0BA2B81144F8}”     StaticName=”RegionGroup=”Custom Lookup ColumnsHidden=”FALSERequired=”FALSESealed=”FALSE/>


Step 2


Add the following method to your feature receiver. This will replace the temporary GUID in the field schema with the actual GUID of the lookup list.


/// <summary>/// Fixes a site column lookup field added through a feature by replacing the temporary GUID in the feature to the actual list GUID.|/// </summary>/// <param>The root web</param>/// <param>The internal field name</param>/// <param>The name of the lookup list</param>/// <param>The temporary GUID that is added in the content type/site column feature XML</param>private void FixLookupSiteColumn(SPWeb rootWeb, string fieldInternalName, string lookupListName, string tempGuid){      SPList lookupList = rootWeb.Lists[lookupListName];            SPFieldLookup field = (SPFieldLookup)rootWeb.Fields.GetFieldByInternalName(fieldInternalName);

      field.SchemaXml = field.SchemaXml.Replace(tempGuid, lookupList.ID.ToString());      field.LookupWebId = rootWeb.ID;



Step 3



Call the method you just added and pass the temporary GUID you added in the Field XML.


public override void FeatureActivated(SPFeatureReceiverProperties properties){            using (SPWeb web = (SPWeb)properties.Feature.Parent)            {                        try

            {                     web.AllowUnsafeUpdates = true;                                       FixLookupSiteColumn(web, “Region”, “Regions”, “B5D4DCEE-61EB-4c53-9A9A-0BA2B81144F8”);}


            {                     web.AllowUnsafeUpdates = false;







Your lookup columns should be created automatically when the feature is activated and link to the correct lookup list.

How to execute SharePoint Timer Jobs immediately

How SharePoint 2010 Will Change The Way Businesses Look At SharePoint