so we had to do teh same thing a little bit ago.
the final call is
var entInst = BcsItem.GetEntityInstance(web.Site, "BEIM.BCS", "BeimBusinessUnit", prepop.BusinessUnit.Id.ToString());
SPBusinessDataField dataField = threeyearlyFileItem.Fields["Business Unit"] as SPBusinessDataField;
threeyearlyFileItem[dataField.RelatedField] = prepop.BusinessUnit.Id.ToString();
BcsItem.SetSecondaryFields(threeyearlyFileItem, dataField, entInst);
You hvae to set teh field then set secondary fields
the class BcsItem does all teh hard work it is as follows
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.SharedService;
namespace BEIM.Webservices
{
public class BcsItem
{
/// <summary>
/// This hits the BCS and gets the instance basaed ont he key and the BCS details
/// </summary>
/// <returns></returns>
public static IEntityInstance GetEntityInstance(SPSite site, string nameSpace, string entityName, string key)
{
BdcService service = SPFarm.Local.Services.GetValue<BdcService>(String.Empty);
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(serviceContext);
IEntity entity = catalog.GetEntity(nameSpace, entityName);
ILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;
IEntityInstance iEInstance = null;
Identity id = new Identity(key);
//entity.GetIdentifiers();
iEInstance = entity.FindSpecific(id, LobSysteminstance);
return iEInstance;
}
/// <summary>
/// Set all secondary BCS fields for a given entity
/// </summary>
/// <param name="listItem">The item to set the fields for</param>
/// <param name="dataField">The BCS field itself</param>
/// <param name="entityInstance">The entity to get the values from</param>
public static void SetSecondaryFields(SPListItem listItem, SPBusinessDataField dataField, IEntityInstance entityInstance)
{
// Convert the entity to a formatted datatable
DataTable dtBDCData = entityInstance.EntityAsFormattedDataTable;
// Set the BCS field itself (Display Value)
listItem[dataField.Id] = dtBDCData.Rows[0][dataField.BdcFieldName].ToString();
// Get the specific finder method to get the columns that returns
IMethodInstance method = entityInstance.Entity.GetMethodInstances(MethodInstanceType.SpecificFinder)[0].Value;
ITypeDescriptorCollection oDescriptors = method.GetReturnTypeDescriptor().GetChildTypeDescriptors()[0].GetChildTypeDescriptors();
// Set the column names to the correct values
foreach (ITypeDescriptor oType in oDescriptors)
{
if (oType.ContainsLocalizedDisplayName())
{
if (dtBDCData.Columns.Contains(oType.Name))
{
dtBDCData.Columns[oType.Name].ColumnName = oType.GetLocalizedDisplayName();
}
}
}
// get the secondary field display names; these should be set
string[] sSecondaryFieldsDisplayNames = dataField.GetSecondaryFieldsNames();
// loop through the fields and set each column to its value
foreach (string columnNameint in sSecondaryFieldsDisplayNames)
{
Guid gFieldID = listItem.Fields[String.Format("{0}: {1}", dataField.Title, columnNameint)].Id;
listItem[gFieldID] = dtBDCData.Rows[0][columnNameint].ToString();
}
}
}
}