none
Can't create a custom Site Column as Lookup type using only CAML RRS feed

  • Question

  • Hello,

    I'm attempting to create a custom Content Type (inherited from Article Page) with custom Site Columns declaratively using CAML and deployed as a feature.  The Feature and everything else deploys fine, and I can successfully set the new Page CT to a new Page Layout.  However, when I go to create a new Publishing Page from that Page Layout with the new custom Content Type (which includes a new custom Lookup site column), then I get an error saying that a value does not fall within the expected range.  Yes, the targeted List does exist and with values (which shouldn't matter anyway, as usually this only produces an empty value, not an error).

    As far as I know, I'm creating this Lookup column exactly as you would for a normal List field, etc.  However, I notice from this old post for SP 2007 that Chris O'Brien records the same problem I'm having now:

    http://www.sharepointnutsandbolts.com/2007/04/creating-list-based-site-columns-as.html

    So, has there been any improvement in SP 2010 such that creating a custom Site Column as type Lookup is possible with normal CAML?

    Thanks!

    Wednesday, August 24, 2011 6:28 PM

Answers

All replies

  • So what I would say is

    • First deploy the list the lookup column is looking values from...  
    • Then deploy the custom site columns of your including the lookup column and others needed
    • then deploy the custom content type...
    • then your custom list using these columns and content type...

    Try this sequence


    "T" | community.zevenseas.com/Blogs/Tanmay/default.aspx
    Wednesday, August 24, 2011 7:39 PM
  • I am doing it this way via the Feature - it's in the above order.  If that is the case, is that the order in which SP sets up each of these components?
    Wednesday, August 24, 2011 7:41 PM
  • I put them in this order in the Feature, but it still does not work.
    Wednesday, August 24, 2011 8:05 PM
  • could you post your CAML up here so we can take a look at it?

    for your "List" property on the field definition are you using a path like /lists/listname or are you using the list's GUID?

    i had some weirdness using the Site.Fields.AddFieldAsXml() method until i started using the GUID of the list in my "List" property.

    Wednesday, August 24, 2011 8:30 PM
  • Yes, Here it is  The Custom Column that is a Lookup Field is in the last code block, the 2nd to last Field Element (called "ExternalNewsSource") - it is this column that is not deploying properly.  If I manually make this column, it exposes the whole Source List and View Field, etc. to manipulation; however, when I auto-provision this, I can see this column, but the Lookup info is greyed out and partially filled in.  As noted above, I have instantiated via the Feature file the items in the order of LookupList CT, Lookup List Definition, Lookup List Instance, Consuming CT and Columns:

    1 - the Lookup List info:

    1a - the Lookup List Elements file:

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <!-- Do not change the value of the Name attribute below. If it does not match the folder name of the List Definition project item, an error will occur when the project is run. -->
     <ListTemplate
       Name="OIGExternalNewsCategoryListDef"
       Type="11002"
       Category="Custom Lists"
       BaseType="0"
       OnQuickLaunch="TRUE"
       SecurityBits="11"
       Sequence="410"
       DisplayName="OIG External News Categories"
       Description="Categories for External News sources."
       Image="/_layouts/images/itgen.png"/>
    </Elements>
    

    1b - the Lookup List Schema file:

    <?xml version="1.0" encoding="utf-8"?>
    <List xmlns:ows="Microsoft SharePoint" Title="OIG External News Categories" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/OIGExternalNewsCategoryListDef" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
     <MetaData>
      <ContentTypes>
       <ContentTypeRef ID="0x01004a9466c6664146c28d16d454ca4fe152" />
      </ContentTypes>
      <Fields>
       <Field ID="{E8D7FED5-A5E0-48A6-98E0-98D0841F1D94}"
           Type="Text"
           Name="ExternalNewsCategory"
           DisplayName="External News Category"
           StaticName="ExternalNewsCategory"
           Required="True"
           SourceID="http://schemas.microsoft.com/sharepoint/v3" />
      </Fields>
      <Views>
       <View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">
        <Toolbar Type="Standard" />
        <XslLink Default="TRUE">main.xsl</XslLink>
        <RowLimit Paged="TRUE">30</RowLimit>
        <ViewFields>
         <FieldRef Name="LinkTitleNoMenu"></FieldRef>
        </ViewFields>
        <Query>
         <OrderBy>
          <FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
         </OrderBy>
        </Query>
        <ParameterBindings>
         <ParameterBinding Name="AddNewAnnouncement" Location="Resource(wss,addnewitem)" />
         <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
         <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_ONET_HOME)" />
        </ParameterBindings>
       </View>
       <View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="AllItems.aspx">
        <Toolbar Type="Standard" />
        <XslLink Default="TRUE">main.xsl</XslLink>
        <RowLimit Paged="TRUE">30</RowLimit>
        <ViewFields>
         <FieldRef Name="ExternalNewsCategory"></FieldRef>
        </ViewFields>
        <Query>
         <OrderBy>
          <FieldRef Name="ID"></FieldRef>
         </OrderBy>
        </Query>
        <ParameterBindings>
         <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
         <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" />
        </ParameterBindings>
       </View>
      </Views>
      <Forms>
       <Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
       <Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
       <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      </Forms>
     </MetaData>
    </List>

    1c - the Lookup List Instance Elements file:

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <ListInstance Title="External News Categories"
            OnQuickLaunch="TRUE"
            TemplateType="11002"
            Url="Lists/ExternalNewsCategories"
            Description="Categories for External News sources.">
      <Data>
       <Rows>
        <Row>
         <Field Name="ExternalNewsCategory">Congressional</Field>
        </Row>
        <Row>
         <Field Name="ExternalNewsCategory">DOI</Field>
        </Row>
        <Row>
         <Field Name="ExternalNewsCategory">IG Community</Field>
        </Row>
        <Row>
         <Field Name="ExternalNewsCategory">IG Testimony</Field>
        </Row>
        <Row>
         <Field Name="ExternalNewsCategory">Media</Field>
        </Row>
        <Row>
         <Field Name="ExternalNewsCategory">OMB</Field>
        </Row>
        <Row>
         <Field Name="ExternalNewsCategory">OPM</Field>
        </Row>
       </Rows>
      </Data>
     </ListInstance>
    </Elements>
    

    2 - the custom Content Type and Columns info (this is inherited from Article Page, so there is no List instance, etc.):

    2a - the Article CT Elements file:

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <!-- Parent ContentType: Article Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D) -->
     <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00f65dfed26f7948aeacfd4541b6794f54"
            Name="OIG News Article"
            Group="DOI OIG"
            Description="Content Type for OIG News Articles"
            Inherits="TRUE"
            Version="0">
      <FieldRefs>
       <FieldRef ID="{532AE911-1D1C-4512-9CE2-DD3EC8E8666D}"
            Name="NewsType"
            DisplayName="News Type"
            Required="TRUE"/>
       <FieldRef ID="{0B1FDF25-5132-4457-B071-67411DEEF940}"
            Name="DisplayLocation"
            DisplayName="Display Location"
            Required="TRUE"/>
       <FieldRef ID="{8C1C2D18-C9E9-4DCC-820C-119E1CFDFC5D}"
            Name="ExternalNewsSource"
            DisplayName="External News Source"
            Required="TRUE"/>
       <FieldRef ID="{8B6FFBA3-67F0-4581-A13F-49F0EA17376C}"
            Name="SliderImage"
            DisplayName="News Slider Image"
            Required="TRUE"/>
      </FieldRefs>
     </ContentType>
    </Elements>
    

    2b - the Article Fields file:

    <?xml version="1.0" encoding="utf-8" ?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     <Field ID="{532AE911-1D1C-4512-9CE2-DD3EC8E8666D}"
         Type="Choice"
         Name="NewsType"
         DisplayName="News Type"
         StaticName="NewsType"
         ShowField="Term1033"
         Required="True"
         EnforceUniqueValues="FALSE"
         Group="DOI OIG"
         ShowInViewForms="TRUE">
      <CHOICES>
       <CHOICE>Internal</CHOICE>
       <CHOICE>External</CHOICE>
      </CHOICES>
     </Field>
     <Field ID="{0B1FDF25-5132-4457-B071-67411DEEF940}"
         Type="Choice"
         Name="LocationDisplay"
         DisplayName="Location Display"
         StaticName="LocationDisplay"
         ShowField="Term1033"
         Required="True"
         EnforceUniqueValues="FALSE"
         Group="DOI OIG"
         ShowInViewForms="TRUE">
      <CHOICES>
       <CHOICE>None</CHOICE>
       <CHOICE>Home Page</CHOICE>
       <CHOICE>News Slider</CHOICE>
      </CHOICES>
     </Field>
     <Field ID="{8C1C2D18-C9E9-4DCC-820C-119E1CFDFC5D}"
         Type="Lookup"
         List="Lists/ExternalNewsCategories"
         Name="ExternalNewsSource"
         DisplayName="External News Source"
         StaticName="ExternalNewsSource"
         ShowField="ExternalNewsCategory"
         Required="True"
         EnforceUniqueValues="FALSE"
         Group="DOI OIG"
         ShowInViewForms="TRUE" />
     <Field ID="{8B6FFBA3-67F0-4581-A13F-49F0EA17376C}"
         Type="URL"
         Format="Image"
         Name="SliderImage"
         DisplayName="News Slider Image"
         StaticName="SliderImage"
         ShowField="Term1033"
         Required="True"
         EnforceUniqueValues="FALSE"
         Group="DOI OIG"
         ShowInViewForms="TRUE" />
    </Elements>

     

    Wednesday, August 24, 2011 8:57 PM
  • In my experience this is probably to do with a missing Overwrite="TRUE" attribute in your site column definition. Check out this blog for a great explanation of some odd behaviour with lookup field provisioning:

    http://www.codefornuts.com/2010/12/how-logic-flaws-in-sharepoints-element.html

    • Marked as answer by sdfsda Wednesday, August 24, 2011 10:14 PM
    Wednesday, August 24, 2011 9:16 PM
  • Holy Crap!  It worked! 

    Just goes to show that SharePoint is an ugly and hideous beast.

    "[It's] a loathsome, offensive brute....yet I cannot look away"

    Wednesday, August 24, 2011 10:14 PM