I cannot seem to find any examples of how to set a BCS External Field via code. I have two lists that both have a BCS field named "Industry". When an item is created in List A I need to create a new item in B and set the value of the industry field to be the same as the value in the new item in list A.
Using ParseAndSetValue only works on the field when it has an initial value. Meaning when the item in list A is created with a value of "Paving" the item in list B is blank even though the code to set it executes. When I update the item in list A and change the value to "Agriculture" again nothing happens (though the code does execute). If I manually set the item in list B to "Paving" and then change the item in list A to "Agriculture" the item in list B is updated to "Agriculture". Here is my example code for said change. This is running in ItemUpdating, the code running in ItemAdded is basically the same.
if (industryChanged)
{
using (new DisabledItemEventsScope())
{
String new_value = properties.AfterProperties[MarketNeedFieldNames.Industry_Internal].ToString();
SPBusinessDataField ind = item.Fields[MarketNeedFieldNames.Industry_Internal] as SPBusinessDataField;
ind.ParseAndSetValue(item, new_value);
item.SystemUpdate();
}
}
Update:
If I go through each item in the list using PowerShell I DO see values for the field in list B but not via the SharePoint UI. The ones with a value visible in the SharePoint UI were all updated via the item picker.
$web = Get-SPWeb "http://dom-dev.domain.net/sites/plm"
$mnlist = $web.Lists["MarketNeed_Priority"]
foreach($item in $mnlist.Items)
{
$item["Industry"] + " - " + $item["Request_ID"]
}
- Earthmoving Excavating - NONE-00001
- Terrain - NONE-00002
- Paving - XXX-00001
- Compact Machine Solutions - NONE-00003
- Paving - NONE-00004
- Compact Machine Solutions - NONE-00005
- Earthmoving Grading - NONE-00006
