Lookup fields

May 9, 2011 at 2:09 PM
Edited May 9, 2011 at 2:19 PM

Hey guys,

When creating a new content type I seem to be unable to add lookup fields.

Am I doing something wrong or is this feature not supported?

Thanks!

Coordinator
May 10, 2011 at 9:58 AM

Hello Saphua,

the automatic deployment of Lookup Columns through XML seems to be a hassle for a lot of SharePoint developers ;-)
Basically the problem is, that the ListInstance from which the column retrieves it's values needs to be created when creating the column in the list.

A lot of blog entries recommend the approach to create lookup columns by code in a feature receiver, but actually that is not neccessary.
You just need to be aware of the required dependencies.

Following approach should work, and can be achieved with the SPSF:

1. Create Feature "Feature1" (prefably Site Scope)
2. Create Site Columns (inlcuding Lookup Columns) and add them to the feature
In the SPSF Wizard the box "Get information from:" needs to contain the relative URL of the Lookup List i.e. /Lists/MyLookupListname
the field "In this column:" requires the internal name of the the column in the lookup list
3. Create all Content Types which use the columns and add them to the feature
4. Create all List Definitions which use the columns and add them to the feature

5. Create a Feature "Feature2" (Web or Site Scope)
6. Add a Feature Dependency to Feature1
7. Add the ListInstances for all Lookup Lists (with the correct relative URLs as specified above)

8. Create a Feature "Feature3" (Web or site Scope)
9. Add a Feature Dependecy to Feature1 and Feature2
10. Add the ListInstance which uses the ContentType with the Lookup Columns

11. Deploy and Activate the Features in the correct order.

I guess, Feature1 and to Feature2 do not have to be separated, but I prefer it that way for a better structure of my solution

Best Regards

Matthias

May 26, 2011 at 10:21 AM

Thanks for your reply Matthias,

I did a setup pretty much like you explained.

I have 4 features:

1. Site Columns

2. Content Types

3. List Instances

4. Content Type Bindings

I can activate feature 1 to 3, but when I activate feature 4 I get this error page:

<nativehr>0x80070057</nativehr><nativestack></nativestack>

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: <nativehr>0x80070057</nativehr><nativestack></nativestack>

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[ArgumentException: <nativehr>0x80070057</nativehr><nativestack></nativestack>]
   Microsoft.SharePoint.Library.SPRequestInternalClass.AddField(String bstrUrl, String bstrListName, String bstrSchemaXml, Int32 grfAdd) +0
   Microsoft.SharePoint.Library.SPRequest.AddField(String bstrUrl, String bstrListName, String bstrSchemaXml, Int32 grfAdd) +153
   Microsoft.SharePoint.SPFieldCollection.AddFieldAsXmlInternal(String schemaXml, Boolean addToDefaultView, SPAddFieldOptions op, Boolean isMigration, Boolean fResetCTCol) +449
   Microsoft.SharePoint.SPContentType.ProvisionFieldOnList(SPField field, Boolean bRecurAllowed) +1547
   Microsoft.SharePoint.SPContentType.ProvisionFieldsOnList() +634
   Microsoft.SharePoint.SPContentType.DeriveContentType(SPContentTypeCollection cts, SPContentType& ctNew) +671
   Microsoft.SharePoint.SPContentTypeCollection.AddContentTypeToList(SPContentType contentType) +2319
   Microsoft.SharePoint.SPContentTypeCollection.AddContentType(SPContentType contentType, Boolean updateResourceFileProperty, Boolean checkName, Boolean setNextChildByte) +242
   Microsoft.SharePoint.SPContentTypeBindingElement.EnsureContentTypeExists(SPWeb web) +2062

[SPException: An error occurred binding content type '0x01001E7103476FD74A34A2773B5F048E91B3' to list '/sites/lmt/Lists/Modules' on 'http://tr3.tripleaconnected.com/sites/lmt'.  Exception '<nativehr>0x80070057</nativehr><nativestack></nativestack>'.]
   Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionContentTypeAndEventReceiverBindings(SPFeaturePropertyCollection props, SPSite site, SPWeb web, Boolean fForce) +23508329
   Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionElements(SPFeaturePropertyCollection props, SPWebApplication webapp, SPSite site, SPWeb web, Boolean fForce) +294
   Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boolean fForce) +25283567
   Microsoft.SharePoint.SPFeatureCollection.AddInternal(SPFeatureDefinition featdef, Version version, SPFeaturePropertyCollection properties, Boolean force, Boolean fMarkOnly) +27339375
   Microsoft.SharePoint.SPFeatureCollection.AddInternalWithName(Guid featureId, String featureName, Version version, SPFeaturePropertyCollection properties, Boolean force, Boolean fMarkOnly, SPFeatureDefinitionScope featdefScope) +150
   Microsoft.SharePoint.SPFeatureCollection.Add(Guid featureId, Boolean force, SPFeatureDefinitionScope featdefScope) +83
   Microsoft.SharePoint.WebControls.FeatureActivator.ActivateFeature(Guid featid, SPFeatureDefinitionScope featdefScope) +699
   Microsoft.SharePoint.WebControls.FeatureActivatorItem.BtnActivateFeature_Click(Object objSender, EventArgs evtargs) +140
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981
Nov 7, 2012 at 5:27 AM
Edited Nov 7, 2012 at 5:28 AM

9 out of 10 times this is because you did not add the attribute Overwrite="TRUE" to the Field node:

Here is an example:

  <Field ID="{GUID}" Name="INTERNALFIELDNAME" DisplayName="Template Item" Type="Lookup" Overwrite="TRUE" List="$Resources:core,lists_Folder;/LISTNAME" WebId="~sitecollection" ShowField="Title" PrependId="TRUE" ></Field>