p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson (http://p2p.wrox.com/forumdisplay.php?f=622)
-   -   Problem setting the value of an external list lookup column. (http://p2p.wrox.com/showthread.php?t=89200)

richv January 21st, 2013 06:52 PM

Problem setting the value of an external list lookup column.
 
Hello all...

I have an external list (based on ECT of course). The list is working great with no problems.
I have created a lookup column based on that external list and attached it to a standard SharePoint list.
All is working with, with one exception.
I need to set the value of the external lookup column programmatically.

I cannot seem to find an example of this.

To summarize, I'm trying to set the value of the external lookup column programmatically on my secondary list.

External List: Customers
External List Lookup Column: CustomerLookup

Standard SharePoint List: Job List
Columns: Title (standard SP Title), JobDate (standard Date Time column), CustomerLookup (lookup column from above).

How do I set the value of the item?

SPListItem item = ....
item["Title"]= "My Text";
item["JobDate"] = somedatevalue;
item["CustomerLookup"] = ??????

Thanks in advance!

ScotHillier January 23rd, 2013 09:17 AM

Setting Value of External Column
 
http://rompenpatrick.wordpress.com/2...l-data-column/

richv January 25th, 2013 12:20 PM

Thanks for the hint...
 
Works great Scott. Thanks for taking the time.

Using the URL, I managed to get it working easy enough. Some of the code from the URL was misleading, so I am putting the code sample here, slightly modified with a little more description.

Code:

class Program
    {

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

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

                    SPList list = web.Lists["TestList"];
                    SPListItem spItem;

                    spItem = list.Items.Add();
                    spItem["Title"] = "My Test Title";

                    // Get a reference to the lookup field
                    SPField myField = spItem.Fields["Customer Lookup"];  // "Customer Lookup" is the display name of the field

                    String lsBCSRelatedFieldName = myField.RelatedField;  // get the name of the actual primary key related field
                   
                    //Set the Entity Instance value
                    int id = 11662;
                    spItem[lsBCSRelatedFieldName] = EntityInstanceIdEncoder.EncodeEntityInstanceId(new object[] { id });
                    //Set the field display value * Please note that the display value is actually unnecessary and can be set with anything.
                    // this will get overwritten eventually from the external content type.
                    spItem["Customer Lookup"] = "Some customer name value.";

                    spItem.Update();
                    list.Update();
                }

            }


        }
    }



All times are GMT -4. The time now is 10:05 PM.

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