View Single Post
  #1 (permalink)  
Old February 1st, 2013, 02:45 PM
richv richv is offline
Authorized User
Points: 95, Level: 1
Points: 95, Level: 1 Points: 95, Level: 1 Points: 95, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2010
Posts: 21
Thanks: 1
Thanked 0 Times in 0 Posts
Default AddDependentLookup and external list lookup columns.

So I have an external content type called Parts. I have created a site column that is a lookup field based on the external list.
The column works great.
Here is the CAML for the external list lookup column.

Code:
  <Field
      Type="BusinessData"
      StaticName="Part_Lookup"
      Name="Part_Lookup"
      DisplayName="Part Lookup"
      Required="FALSE"
      EnforceUniqueValues="FALSE"
      Group="BCS Lookup"
      ID="{E6C4D16E-6F70-4BFC-A989-B097C782C12B}"
      SystemInstance="BCT_BCSInstance"
      EntityNamespace="BCT_BCS"
      EntityName="Part"
      BdcField="sName"
      Profile=""
      HasActions="True"
      SecondaryFieldBdcNames="0"
      RelatedField="Part_ID"
      SecondaryFieldWssNames="0"
      RelatedFieldBDCField=""
      RelatedFieldWssStaticName="Part_ID"
      SecondaryFieldsWssStaticNames="0"
      AddFieldOption="AddFieldInternalNameHint"
      />
I have attempted through CAML unsuccessfully (long ago) to add a projected field to this lookup column. This field is the sPartCode field in the ECT.

I can, using the SharePoint GUI add this projected field and it works. I want to add this programmatically rather than manually through the GUI.

I used the following site to come up with something that could add the projected column (using AddDependentLookup) as follows:
http://www.c-sharpcorner.com/uploadf...arepoint-2010/

When I execute the "AddDependentLookup" line of code, I get this error message.
Code:
Microsoft.SharePoint.SPException was unhandled
  Message=The primary lookup field is not valid.  It may have been deleted or is not a primary lookup field.   Create a new lookup field referencing another primary lookup field that is valid.
  Source=Microsoft.SharePoint
  ErrorCode=-2146232832
  StackTrace:
       at Microsoft.SharePoint.SPFieldCollection.AddDependentLookupInternal(String displayName, Guid primaryLookupFieldId, String strShowField, SPAddFieldOptions op)
       at Microsoft.SharePoint.SPFieldCollection.AddDependentLookup(String displayName, Guid primaryLookupFieldId)
       at AddProjectedColumn.Program.Main(String[] args) in C:\source\Blackcreek\SharePointUtils\AddProjectedColumn\AddProjectedColumn\AddProjectedColumn\Program.cs:line 42
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Here is the code I'm attempting to use.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace AddProjectedColumn
{
    class Program
    {

        private static string _sSiteURL = @"http://myserver/sites/renewals2";

        static void Main(string[] args)
        {
            using (SPSite site = new SPSite(_sSiteURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    Console.WriteLine("Site: " + site.Url);
                    // do stuff here.

                    SPList lookupList = web.Lists.TryGetList("Part List");
                    SPList list = web.Lists.TryGetList("TestList");

                    // Get the field object of the original field.
                    
                    SPField partLookup = null;
                    partLookup = list.Fields.TryGetFieldByStaticName("Part_Lookup");


                    Console.WriteLine(String.Format("The field name is : {0}", partLookup.ToString()));

                    if  (partLookup != null)
                    {
                        string lsProjectedCol = web.Fields.AddDependentLookup("Part_Lookup-SKU", partLookup.Id);
                        SPFieldLookup projectedCol = (SPFieldLookup)web.Fields.GetFieldByInternalName(lsProjectedCol);
                        projectedCol.LookupField = lookupList.Fields["sPartCode"].InternalName;
                        projectedCol.Update();

                    }

                }

            }


        }
    }
}
Any ideas where I'm going wrong here?