locked
create lookup column in list template without guid of parent list RRS feed

  • Question

  • Hi,

    i am trying to create a lookup column in my custom list using custom list template schema.

    i got a thread in this forum regarding this, which says if you want to hook up to a column of different list, you need to know guid for the list.

    my question is, is it necessary to know guid of parent list or is there nay alternative?

    i found that instead of list guid if i give attirbute- List="Lists/Sub Category" and give parent list name will solve the purpose. but it doesnt seems to work.

    My approach:

    Approach 1: Created custom content type.

    <ContentType ID="0x0100b9699db308664bc48127309bcbbc848a"
        Name="MyCT"
        Group="Development"
        Description="Developing Content Type"
        Version="0">
        <FieldRefs>
          <FieldRef ID="{C134EDDE-501F-4ba9-A203-A6885D52BA4A}" Name="MyCTField" />
        </FieldRefs>
      </ContentType>
      <Field ID="{C134EDDE-501F-4ba9-A203-A6885D52BA4A}"
      Type="Lookup"
      Name="SubCategoryName"
      List="Lists/Sub Category"
      DisplayName="SubCategoryName"
      StaticName="SubCategoryName"
      Hidden="FALSE"
      Required="TRUE"
      Sealed="TRUE" />

    when i try to add this content type to list i get error.please tell me what might be wrong in above code.

    Approach 2: changing list schema directly to implement lookup

    <ContentType
            ID="0x010099FE4F3ACD494e30A36693F9EE65BAF2"
            Name="Classification Item"
            Group="$Resources:List_Content_Types"
            Description="Classification item content type."
            Version="0">
            <FieldRefs>       

              <!--Add a field reference for the custom field.-->
              <FieldRef
               ID="{4C4A1A22-7AB1-42db-B3C4-CC637CB7E899}"
               Name="CategoryName"
               Required="TRUE"
               ShowInNewForm="TRUE"
               ShowInEditForm="TRUE"/>

            </FieldRefs>

    ......

    <Field Type="Lookup"
                 DisplayName="CategoryName"
                 Required="FALSE"
                 List="Lists/Category"
                 ShowField="CategoryName"
                 UnlimitedLengthInDocumentLibrary="FALSE"
                 ID="{29F4B3CA-6306-4d61-AEF8-698E584ACA6F}"             
                 StaticName="CategoryName"
                 Name="CategoryName"
                 ColName="int2"
                 RowOrdinal="0" />
        </Fields>

    when i creat a list using this template i get a dropdown but i dont get look up content from it parent list "Category". what might be wrong in above code.

    Please help.

    Thanks in advance.

     

     


    ullasjoshi
    Wednesday, May 12, 2010 11:02 AM

Answers

All replies

  • The only way I have seen (and used) this succesfully is by using a feature receiver to hook up the list to the lookup column, after the lookup list and column has been created: this post http://www.sharepointnutsandbolts.com/2007/04/sample-code-creating-list-based-site.html describes this method in greater detail
    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.

    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010 Twitter: @starznet
    Wednesday, May 12, 2010 11:18 AM
  • Thank Stuart, i will explore on this method of implementation.

    But is there any other way of implemenation? basically i am looking for implementing look up by modifying the schema or by content types.


    ullasjoshi
    Thursday, May 13, 2010 5:40 AM
  • Hi ullasjoshi,

    Here is a post that describes how to create lookup field declaratively based on the list name. Please, try to follow that step-by-step (e.g. define site columns in fields.xml first).

    Add SharePoint lookup column declaratively through CAML XML
    http://blogs.msdn.com/joshuag/archive/2008/03/14/add-sharepoint-lookup-column-declaratively-through-caml-xml.aspx

    If it still does not work, try to use a list without space in its name.

    Hope it helps.

    Peter

    Thursday, May 13, 2010 6:22 AM
  • Thanks Peter, i will explore on the same.

    Please let me know what might have gone wrong in above approach, that its not working(i have mention my approaches above approach 1 and approach 2)

     


    ullasjoshi
    Thursday, May 13, 2010 9:57 AM
  •  as per the link

    http://stackoverflow.com/questions/1139008/lookup-field-as-a-site-column-via-caml

    approach 1 is similar and the user is getteing simlar error that of mine.

    as per the feed back given in the link, when attaching the content we should make sure the list on which content type is build should already be present. in my case list is already present.

    it also suggest to check the log file and surprisingly there is no trace of content type error in log.


    ullasjoshi
    Thursday, May 13, 2010 11:14 AM
  • Hi,

    I've successfully created a lookup list using the list name (Tasks) declaratively through a feature on a standard team site (so the list is an existing one).

    Here are the most important parts of the code:

    ...

    <ContentTypes>
          <ContentType  ID="0x01"
            Name="Item"
            Group="Item group"
            Description="Item description"
            Version="0">
            <FieldRefs>
                <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Required="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE"/> <!-- Title -->
                <FieldRef ID="{95d89725-eb97-428b-bc79-ee02ca8b721b}" Name="TestField" Required="FALSE" ShowInNewForm="TRUE" ShowInEditForm="TRUE"/> <!-- Test field -->
            </FieldRefs>
            <XmlDocuments>
                <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
                    <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
                        <Display>ListForm</Display>
                        <Edit>ListForm</Edit>
                        <New>ListForm</New>
                    </FormTemplates>
                </XmlDocument>
            </XmlDocuments>
        </ContentType>
          <ContentTypeRef ID="0x0120" />
        </ContentTypes>
        <Fields>
          <Field ID="{95d89725-eb97-428b-bc79-ee02ca8b721b}"
              Name="TestField"
              SourceID="http://schemas.microsoft.com/sharepoint/v3"
              StaticName="TestField"
              Group="Test site columns"
              ReadOnly="FALSE"
              Hidden= "FALSE"
              Type="Lookup"
              DisplayName="Test field"
              List="Lists/Tasks"
              FieldRef="ID"
              ShowField="Title" />
        </Fields>

    ...

            <ViewFields>
              <FieldRef Name="Attachments">
              </FieldRef>
              <FieldRef Name="LinkTitle">
              </FieldRef>
              <FieldRef Name="TestField">
              </FieldRef>
            </ViewFields>

    ...

    You can achieve similar result by creating the lookup field as site column first, then re-using the same site column in the list definition. I've tested that option either.

    I plan to write a blog post about this including the full "code" of the feature. Will update this thread when it's pulished.

    Peter

    Thursday, May 13, 2010 1:33 PM
  • Hi,

    I've published my post about this issue. I hope it will help:

    Declaratively adding a lookup field to a list schema using the name of the referenced list
    http://pholpar.wordpress.com/2010/05/14/declaratively-adding-a-lookup-field-to-a-list-schema-using-the-name-of-the-referenced-list/

    Peter

    Friday, May 14, 2010 12:35 AM
  • Thanks Peter,

    i will try this out and let you know.

     


    ullasjoshi
    Monday, May 17, 2010 4:53 AM