tags:

views:

1633

answers:

6

I'm trying to create a Lookup Field as a Site Column via CAML. The list I want to use as the source of the lookup is created in the Feature Receiver so I don't know it's ID. I've read several blog posts that indicate that I can just put the path to the list in the List attribute. It seems from the comments on these post that this solution works for some people but not for others. I'm in the latter group.

When I try to associate a content type that uses the lookup site column I: "Exception from HRESULT: 0x80040E07"

<Field 
  ID="{da94e56b-428f-4b95-b4c6-24aed0256475}" 
  Name="Test_x0020_Lookup_x0020_Column"
  StaticName="Test_x0020_Lookup_x0020_Column" 
  DisplayName="Test Lookup Column" 
  Type="Lookup" 
  Required="FALSE" 
  List="Lists/Test" 
  ShowField="Title" 
  PrependId="TRUE"
  Group="Test Site Columns" />

  <ContentType
    ID="0x0100B6D92594DDCE8E479D0EB0C414C463B0"
    Name="Test Lookup Content Type"
    Version="0"
    Group="Test Content Types">
    <FieldRefs>
      <FieldRef 
         ID="{da94e56b-428f-4b95-b4c6-24aed0256475}" 
         Name="Test_x0020_Lookup_x0020_Column" 
         Required="TRUE" />
    </FieldRefs>
  </ContentType>
+1  A: 

Putting Path to the list in the List attribute does work I have done it in my Project and was successful. One thing i wanted you to check is order of the feature. Are you executing the Features Manually ? or using them in the onet.xml . Because when your content type feature executes there should already be that list to refer.

Also you should get more verbose information in the SharePoint logs that is present in the Logs folder. Try to find more information of the error there it will help in many cases.

Kusek
A: 

I had the same problem with a list defined in schema.xml. I fixed it by ensuring the same path is supplied in both the field def and the schema.xml file.

Michhes
A: 

I have the same error, except my field is being referred to within a Content Type. Does anyone know how to fix this?!

zikoziko
A: 

Hi, try this http://cipriangrosu.blogspot.com/2008/12/lookup-column-guid-of-list-created.html worked for me. And yes, you have to be sure that the list already exist. If the list is not created yet you can create a thread that will add the proper GUID after the list will be created.

Ciprian Grosu
+1  A: 

The List="" should be a unique GUID. Since you don't know the GUID yet, you will need to programmatically replace it when the list is created.

Here is some information on how to do that: http://www.sharepointtactics.com/blog/fix-lookup-fields-created-through-features.html

Eric
A: 

The list name does not work for me either.

I had to create the list and lookup column dynamically with a Feature Activator. Once I create the lookup List via SharePoint object model, you have the Guid. Then you can create the column dynamically on the list requiring the lookup column using the existing CAML above with the following method:


string xml = // your CAML up top with a TOKEN to replace List="{TOKEN}"


SPList listWithLookupColumn = web
            .Lists
            .Add(url,description,SPListTemplateType.GenericList);

Guid listId  = listWithLookupColumn.ID;

xml = xml.Replace("TOKEN",listId);

// some code to find the list you want lookup column on
SPList listToAddLookupColumn = listRepository(listNameToFind); 

listToAddLookupColumn
    .AddFieldAsXml(xml,true,SPAddFieldOptions.AddToDefaultContentType)

Joey V.