{"id":215,"date":"2010-12-03T10:08:33","date_gmt":"2010-12-03T08:08:33","guid":{"rendered":"http:\/\/jsiegmund.wordpress.com\/?p=215"},"modified":"2013-04-18T18:42:54","modified_gmt":"2013-04-18T17:42:54","slug":"sp2010-setting-bcs-column-and-related-fields","status":"publish","type":"post","link":"http:\/\/blog.repsaj.nl\/index.php\/2010\/12\/sp2010-setting-bcs-column-and-related-fields\/","title":{"rendered":"SP2010: Setting BCS column and related fields"},"content":{"rendered":"<p>I&#8217;ve been struggling for a while to programmatically set BCS columns. Setting the column itself isn&#8217;t that hard, but it&#8217;s getting all the related fields to show data too that&#8217;s a challenge. There seems to be no out of the box method to do this, so I&#8217;ve created one myself. Without further ado, let&#8217;s get to the code!<\/p>\n<p>[sourcecode language=&#8221;csharp&#8221;]<br \/>\n\/\/\/ &lt;summary&gt;<br \/>\n\/\/\/ Set all secondary BCS fields for a given entity<br \/>\n\/\/\/ &lt;\/summary&gt;<br \/>\n\/\/\/ &lt;param name=&#8221;listItem&#8221;&gt;The item to set the fields for&lt;\/param&gt;<br \/>\n\/\/\/ &lt;param name=&#8221;dataField&#8221;&gt;The BCS field itself&lt;\/param&gt;<br \/>\n\/\/\/ &lt;param name=&#8221;entityInstance&#8221;&gt;The entity to get the values from&lt;\/param&gt;<br \/>\nprivate static void SetSecondaryFields(SPListItem listItem, SPBusinessDataField dataField, IEntityInstance entityInstance)<br \/>\n{<br \/>\n\/\/ Convert the entity to a formatted datatable<br \/>\nDataTable dtBDCData = entityInstance.EntityAsFormattedDataTable;<\/p>\n<p>\/\/ Set the BCS field itself (Display Value)<br \/>\nlistItem[dataField.Id] = dtBDCData.Rows[0][dataField.BdcFieldName].ToString();<\/p>\n<p>\/\/ Get the specific finder method to get the columns that returns<br \/>\nIMethodInstance method = entityInstance.Entity.GetMethodInstances(MethodInstanceType.SpecificFinder)[0].Value;<br \/>\nITypeDescriptorCollection oDescriptors = method.GetReturnTypeDescriptor().GetChildTypeDescriptors()[0].GetChildTypeDescriptors();<\/p>\n<p>\/\/ Set the column names to the correct values<br \/>\nforeach (ITypeDescriptor oType in oDescriptors)<br \/>\n{<br \/>\nif (oType.ContainsLocalizedDisplayName())<br \/>\n{<br \/>\nif (dtBDCData.Columns.Contains(oType.Name))<br \/>\n{<br \/>\ndtBDCData.Columns[oType.Name].ColumnName = oType.GetLocalizedDisplayName();<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ get the secondary field display names; these should be set<br \/>\nstring[] sSecondaryFieldsDisplayNames = dataField.GetSecondaryFieldsNames();<\/p>\n<p>\/\/ loop through the fields and set each column to its value<br \/>\nforeach (string columnNameint in sSecondaryFieldsDisplayNames)<br \/>\n{<br \/>\nGuid gFieldID = listItem.Fields[String.Format(&#8220;{0}: {1}&#8221;, dataField.Title, columnNameint)].Id;<br \/>\nlistItem[gFieldID] = dtBDCData.Rows[0][columnNameint].ToString();<br \/>\n}<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>To use this to update a list item, call it like this:<\/p>\n<p>[sourcecode language=&#8221;csharp&#8221;]<br \/>\nSPBusinessDataField dataField = listItem.Fields[&#8220;BCSField&#8221;] as SPBusinessDataField;<br \/>\nlistItem[dataField.RelatedField] = customerId;<br \/>\nSetSecondaryFields(listItem, dataField, entityinst);<br \/>\nlistItem.UpdateOverwriteVersion();<br \/>\n[\/sourcecode]<\/p>\n<p>Special thanks to Wade A Hunter for providing the guidelines on how to do this in 2007, this code is based on that. See: <a href=\"http:\/\/wadehuntersblog.blogspot.com\/2010\/10\/working-with-sharepoint-2007-business.html\">http:\/\/wadehuntersblog.blogspot.com\/2010\/10\/working-with-sharepoint-2007-business.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been struggling for a while to programmatically set BCS columns. Setting the column itself isn&#8217;t that hard, but it&#8217;s getting all the related fields to show data too that&#8217;s a challenge. There seems to be no out of the box method to do this, so I&#8217;ve created one myself. Without further ado, let&#8217;s get<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[34],"tags":[11,7],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3KFR1-3t","_links":{"self":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts\/215"}],"collection":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":0,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}