Programmatically setting BCS External Data column
If you are working with external columns, you will maybe, sooner or later, try to set programmatically a BCS external data column.
It takes me some times to understand how to achieve that, but I have now the answer.
// Allow Unsafe Updates on the current SPWeb object
curWeb.AllowUnsafeUpdates = true;
// -- write into the BCS field --
// Get a reference to the field
SPField myField = newItem.Fields["BCSField"];
//Get the entity Name for the field
XmlDocument xmlData = new XmlDocument();
xmlData.LoadXml(myField.SchemaXml);
String entityName = xmlData.FirstChild.Attributes["RelatedFieldWssStaticName"].Value;
//Set the Entity Instance value
newItem[entityName] = EntityInstanceIdEncoder.EncodeEntityInstanceId(new object[] { New_Value });
//Set the field display value
newItem["BCSField"] = New_Value;
// save the new item
newItem.Update();
// allow unsafe updates to false
curWeb.AllowUnsafeUpdates = false;
You just have to replace the New_Value into the code by your (string) value !
