Creating your own custom field types in Windows SharePoint Service v3 is an awesome thing. Instead of relying on the out-of-the-box types (text, number, Url, etc), you can create your own fields that incorporate
whatever logic and data you want. You can derive from the SPField class or a specific type of field (such as SPFieldText, SPFieldNumber, etc).
A custom field can have some custom properties. For example, if you have a custom field named Car, you could have a custom property named Color on the Car field. The user could select a
color from a list of choices when creating their Car column in SharePoint. This is accomplished by implementing your custom property itself as a custom field. That's right - both Car and Color would inherit from SPField
in order for Car to have a custom property of the Color type.
There's a great MSDN forum thread that covers this topic: Custom Field Type Properties. Thanks to
Neil for providing me the link earlier.
If you use an out-of-the-box field type for your custom property, everything will work fine:
<!-- ...partial field type definition -->
<propertyschema>
<fields>
<field name="MyCustomProperty"
displayname="My Custom Property"
type="Text">
</field>
</fields>
</propertyschema>
However, if you use a custom field for your custom property, AND if your custom field inherits directly from SPField, it won't work:
<!-- ...partial field type definition -->
<propertyschema>
<fields>
<field name="MyCustomProperty"
displayname="My Custom Property"
type="Color">
</field>
</fields>
</propertyschema>
//Color class
public class ColorField : SPField
{ ... }
This results in a generic "Unknown Error" message in your browser when you attempt to create your Car field.
The answer is to override and implement the FieldValueType property on your custom field class:
//Color class
public class ColorField: SPField
{
public override Type FieldValueType {
get {
//return your fancy type
return typeof(string);
}
}
}
Upon inspecting the SPField class in Reflector, the FieldValueType getter returns null all the time. Specific implementations of SPField (such as SPFieldText) return a non-null value. If you choose to inherit
from a specific SPField type such as SPFieldText or SPFieldNumber, you shouldn't run in to this problem.
tags: wss customfields code programming