Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

enter image description here

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Hope following article will helps,

http://rompenpatrick.wordpress.com/2012/07/19/programmatically-setting-bcs-external-data-column/

When you create a BCS based, external data column within a SharePoint list there is another, hidden field created for you. This is given the name yourFieldName_ID so that if your field is called "Industry" the hidden field is called Industry_ID. This holds the BCS entity id for the entity in question. The name of this field is also stored within the SchemaXml property of the BusinessData field. You can set it my using this extension method:

    /// <summary>
    /// Sets the value of an SPBusinessDataField to the newValue.
    /// </summary>
    /// <param name="item"></param>
    /// <param name="fieldInternalName"></param>
    /// <param name="newValue"></param>
    public static void SetExternalFieldValue(this SPListItem item, string fieldInternalName, string newValue)
    {
        if (item.Fields[fieldInternalName].TypeAsString == "BusinessData")
        {
            SPField myField = item.Fields[fieldInternalName];
            XmlDocument xmlData = new XmlDocument();
            xmlData.LoadXml(myField.SchemaXml);
            //Get teh internal name of the SPBusinessDataField's identity column.
            String entityName = xmlData.FirstChild.Attributes["RelatedFieldWssStaticName"].Value;

            //Set the value of the identity column.
            item[entityName] = EntityInstanceIdEncoder.EncodeEntityInstanceId(new object[] { newValue });
            item[fieldInternalName] = newValue;
        }
        else
        {
            throw new InvalidOperationException(fieldInternalName + " is not of type BusinessData");
        }
    }
share|improve this answer
    
I also came across that in my Googling. The vast majority of the code in that example is for setting a secondary, related field. I'm having issues just setting the value of the primary BCS field. The way he does that in his example was actually how I initially tried and the code did not work. –  Robert Kaucher Nov 6 '12 at 15:16
    
Since there was no explanation of that code on the page I honestly did not understand it fully. Once I figured out the larger issue I realized that code was likely correct. I have added an extension method that I created to your answer and will select it as my pick. –  Robert Kaucher Nov 6 '12 at 19:28

try this code for setting external field in sharepoint list :

// "Project ID" is the BDC column in list
SPBusinessDataField dataField1 = oSPFile.Item.Fields["Project ID"] as SPBusinessDataField; 

dataField1.ParseAndSetValue(oSPFile.Item, ProjectID.ToString());  //Set your value here

oSPFile.Item[dataField1.RelatedField] = oSPFile.Item.Fields["Project ID"];
share|improve this answer
    
This code only works if the field I am changing already has a value; which makes no sense. So that when the item in List A is created the corresponding item in list B does not get a value set. IF I then attempt to change the item in list A again no value is set in list B. But if I manually set the value on the item in list B and then alter the item in list A the change is made. –  Robert Kaucher Nov 6 '12 at 15:01
    
Actually my previous comment is not entirely true. I have revised my question. –  Robert Kaucher Nov 6 '12 at 17:25

Thanks to Brian Abrams on the MSDN forums I have figured this out. When you create a BCS based, external data column within a SharePoint list there is another, hiddne field created for you. This is given the name yourFieldName_ID so that if your field is called "Industry" the hidden field is called Industry_ID. This holds the BCS entity id for the entity in question. Basically all I did was update this column with the value from the original list and the items display properly in the SharePoint GUI.

IMO, in addition to being poorly documented this is just simply horrible. The purpose of having an API is to abstract away these sorts of complexities. It also means that the ACID principles are just simply contrived within an EDC. I can quite clearly set the ID and the text value to completely different things.

String new_value = properties.AfterProperties[MarketNeedFieldNames.Industry_Internal].ToString();
SPBusinessDataField ind = (SPBusinessDataField)item.Fields[MarketNeedFieldNames.Industry_Internal];
ind.ParseAndSetValue(item, new_value);
item[MarketNeedFieldNames.Industry_Internal + "_ID"] = properties.AfterProperties[MarketNeedFieldNames.Industry_Internal + "_ID"].ToString();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.