Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson
This is the forum to discuss the Wrox book Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson; ISBN: 978-0-470-61790-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 1st, 2013, 01:45 PM
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?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem setting the value of an external list lookup column. richv BOOK: Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson 2 January 25th, 2013 11:20 AM
The created external list does not support updating existing items Treffe BOOK: Beginning SharePoint 2010 Development 5 August 2nd, 2010 11:56 AM
Permissions issue on Create External List - resolved kendo666 BOOK: Beginning SharePoint 2010 Administration: Windows SharePoint Foundation 2010 and Microsoft Sha 0 July 27th, 2010 09:26 AM
Lookup max with criteria in multiple columns cc16 Excel VBA 2 September 6th, 2006 05:48 AM
List Mutiple Columns? Vendlus Reporting Services 0 April 28th, 2006 07:51 AM



All times are GMT -4. The time now is 08:05 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.