Updating external data column using client object model

Business Connectivity Services provides read/write access to external data from LOB systems, Web services, databases, and other external systems within Microsoft SharePoint 2010 and Microsoft Office 2010 applications. We can create external data column in any list to provide lookup to external data.

There might be requirement to update the external data column using client obejct model. We must consider few things before updating external data colum. Let's first have a look field xml for external column -

Sample field xml would look something like the one mentioned below. Its not the completed field xml but portion of it with information needed to update it using client object mode.

<Field Type="BusinessData"
       DisplayName="SampleCol"
       Required="FALSE"
       ID="{4a8eef87-0f06-44c8-8163-20d377c611a0}"
       SourceID="{4a9352ac-28ba-4233-8c73-90622bb896c2}"       
       StaticName="BusinessContact"
       BaseRenderingType="Text"
       RelatedField="Sample_Data_ID" ......../>

For each external data column, one hidden column gets created. Name of the hidden column can be found using this field xml. RelatedField contains the name of hidden field for given external data column.

To update the external data column using client object mode, we need to update both the column.

1) External data column

2) Hidden column for external data column

So using clientcontext we can easily get the list item to be updated. I am not going into details of how to get listitem to be updated. Assuming, we have listitem to be updated, to update the list item we need to do -

listItem["SampleCol"] = "abc";

listItem["Sample_Data_ID"] = EntityInstanceIdEncoder.EncodeEntityInstanceId(new object[] { "abc" });

listItem.update();

Assembly containing EncodeEntityInstanceId method might not be available in the client machine where this code is getting run, so can write service and get data be encode at server and send to client or if assembly is available then we can directly refer it.