Custom Multicolumn Field Classes
Last modified: April 03, 2010
Applies to: SharePoint Foundation 2010
SharePoint Foundation contains a class, SPFieldMultiColumn, that emulates a field containing multiple values. In actuality, the class contains a single string value, in which the values of the various "columns" are separated by special delimiter characters. The class contains members that enable you to manipulate the class as if it contained multiple columns; for example, setting the number of "columns", and setting or retrieving the individual "column" values.
The SPFieldMultiColumn class contains a class, SPFieldMultiColumnValue, which represents the actual "column" values in the SPFieldMultiColumn object. The SPFieldMultiColumnValue class includes an index that developers should use to access the value of a specific "column", rather than directly parsing the results of the ToString() method.
SPFieldMultiColumnValue Class Constructors
The SPFieldMultiColumnValue class includes three constructor methods. Developers can decide which constructor to use based on the following behavior:
SPFieldMultiColumnValue() Creates a new SPFieldMultiColumnValue object, without a set number of "columns". The developer can then use the Add(String) method to append any number of "columns" of data necessary onto the object.
SPFieldMultiColumnValue(Int32) Creates a new SPFieldMultiColumnValue object with the specified number of "columns." Developers can then use the Item property to get and set the value of a specific "column."
SPFieldMultiColumnValue(String) Creates a new SPFieldMultiColumnValue object, using the passed-in string argument to determine the number of "columns" and the value in each. The string argument must begin and end with the delimiter characters ";#" and it must separate each field with them as well.
For example, suppose you call this constructor, passing the following string:
";#column one value;#column two value;#column three value;#"
The constructor creates a new SPFieldMultiColumnValue object with three "columns" of data. The first contains "column one value"; the second, "column two value"; and the third, "column three value".
Developers can then use the Item property to get and set the value of a specific "column".
This constructor is useful when reading the string from a database or other data source where the column values are stored with the delimiting characters already in place.
Writing Values from the SPFieldMultiColumnValue Class
Conceptually, the final SPFieldMultiColumnValue constructor performs the opposite function of the ToString() method. The ToString() method generates a string, complete with delimiting characters for "column" separation, from the SPFieldMultiColumnValue object. This method is useful when writing the "column" values to a database or other data source where the "column" values are stored as a string, with the delimiting characters already in place.