View Single Post
  #3 (permalink)  
Old January 25th, 2013, 12:20 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 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.

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.";