SharePoint: Working with BDC Secondary Fields
As you probably know, in SharePoint 2010 Business Data Connectivity replaced Business Data Catalog of SharePoint 2007. Some changes affects how Business Data Columns are presented in a list’s schema. In SP 2007 a declaration of a Business Data Column in a schema.xml may look like the following:
<Field Type="BusinessData" DisplayName="Product" Required="FALSE" ID="{bc203358-6113-470f-9b08-f6100cc034f2}" StaticName="Product" BaseRenderingType="Text" Name="Product" SystemInstance="ExternalProductDB_Instance" Entity="Products" BdcField="Name" Profile="" HasActions="False" RelatedField="Products_ID" RelatedFieldBDCField="" RelatedFieldWssStaticName="Products_ID" SecondaryFieldBdcNames="Price:Producer" SecondaryFieldWssNames="Product_x003a__x0020_Price:Product_x003a__x0020_Producer" SecondaryFieldsWssStaticNames="Product_x003a__x0020_Price:Product_x003a__x0020_Producer" />
In contrast, in SP 2010 it looks like
<Field Type="BusinessData" DisplayName="Product" Required="FALSE" ID="{bc203358-6113-470f-9b08-f6100cc034f2}" StaticName="Product" BaseRenderingType="Text" Name="Product" SystemInstance="ExternalProductDB_Instance" Entity="Products" BdcField="Name" Profile="" HasActions="False" RelatedField="Products_ID" RelatedFieldBDCField="" RelatedFieldWssStaticName="Products_ID" SecondaryFieldBdcNames="6%209%20Price%20Producer%204" SecondaryFieldWssNames="27%2030%20Product%5Fx003a%5F%5Fx0020%5FPrice%20Product%5Fx003a%5F%5Fx0020%5FProducer%206" SecondaryFieldsWssStaticNames="27%2030%20Product%5Fx003a%5F%5Fx0020%5FPrice%20Product%5Fx003a%5F%5Fx0020%5FProducer%206" />
Undoubtedly, in SP 2010 the secondary fields became practically unreadable. Indeed, the format of secondary fields‘ presentation is revised. Moreover some kind of URL encoding are applied to them. Let’s examine how these secondary fields could look before the URL encoding is applied:
<Field ... SecondaryFieldBdcNames="6 9 Price Producer 4" SecondaryFieldWssNames="27 30 Product_x003a__x0020_Price Product_x003a__x0020_Producer 6" SecondaryFieldsWssStaticNames="27 30 Product_x003a__x0020_Price Product_x003a__x0020_Producer 6" />
Now it’s pretty easy to figure out the new format. Take a look at the SecondaryFieldBdcNames attribute. It contains names of two secondary bdc fields: ‘Price’ and ‘Producer’. 6 is the length of the ‘Price’ name + 1 for a space character right after the name. 9 is the length of the ‘Procuder’ name + 1 for a space character after the name. 4 is the length of the sub-string ‘6 9 ‘ (including spaces), which contains the lengths of the fields’ names. See a picture below:
Note that the SecondaryFieldBdcNames, SecondaryFieldWssNames and SecondaryFieldsWssStaticNames have the same format.
We have a lot of code interacting with Business Data Columns, thus we were interested in means allowing easily to decode, encode and parse Secondary Fields attributes. In the Microsoft.SharePoint.dll, there is the internal BdcClientUtil class containing the basic methods to work with Secondary Fields:
internal class BdcClientUtil { ... string[] SplitStrings(string combinedEncoded); string CombineStrings(string[] strings); ... }
So, using .Net Reflector I’ve extracted these methods along with several others auxiliary ones and put them into the helper-class called SecondaryFieldNamesHelper. All internal methods and properties were honestly stolen from Microsoft.SharePoint.dll, the public ones were added by me and described below:
- string Encode(string[] secondaryFieldNames) – accepts an array of field names and returns the string formatted and encoded according to the SharePoint 2010 requirements;
- string[] Decode(string str) – accepts an encoded string, decodes it and returns a resultant array of field names;
- bool IsEncodedString(string str) – checks whether a passed string is encoded;
- string ConvertToSP2010(string str) – converts a SP 2007 colon-separated string of secondary fields into another one formatted and encoded according to the SharePoint 2010 requirements;
Below is the source code of the SecondaryFieldNamesHelper:
The SecondaryFieldNamesHelper can be used as shown below:
SPBusinessDataField bdcField = ... string secondaryFieldWssNamesProperty = bdcField.GetProperty("SecondaryFieldWssNames"); string[] secondaryWssFieldNames = SecondaryFieldNamesHelper.Decode(property); string secondaryFieldBdcNamesProperty = bdcField.GetProperty("SecondaryFieldBdcNames"); string[] secondaryFieldBdcNames = SecondaryFieldNamesHelper.Decode(secondaryFieldBdcNamesProperty); string sp2010WssStaticNames = SecondaryFieldNamesHelper.ConvertToSP2010("Product_x003a__x0020_Price:Product_x003a__x0020_Producer");
As a .cs file the SecondaryFieldNamesHelper class is available here.