Home > .Net, SharePoint 2010 > Programmatically setting BCS External Data column

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 !

About these ads
Categories: .Net, SharePoint 2010
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: