HOW TO: Create a Custom SharePoint Field Control and Field Type 

With WSS 3.0 / MOSS 2007 it is now possible to create custom field types and field control. These constructs are exciting in that they can be used to expose complex data types in SharePoint site columns. For example, if you'd like to capture and store structured data such as an address, a social security number or a phone number that is now possible with a few custom code pieces. In fact the default SharePoint fields such as text, lookup and choice all use this same infrastructure to exposes data to SharePoint lists and page layouts.

The field type class is the native field type implementation that is used to create new site columns and list fields.

Read more here:
Custom Field Type Definition
How to: Create a Custom Field Type and Field Control


Here's how I create a custom field type and field control:

  • Create the Field Control class
    This class is a server control that is used by the edit form or placed on a page layout to exposes data in your custom field type. It includes any formatting necessary for "view" mode and any input controls necessary for "edit" mode.

    In this example I am creating a simple field control that exposes a text field type with minimal formatting and a text input box for editing. Note the "Value" property override, this property is used by the SharePoint framework to get the value of the field when editing in a page layout or item edit form.


    Server Control Class:
    public class MyCustomFieldControl : BaseFieldControl {
    
        private DropDownList _editor;
        private Literal _litvalue;
    
        public override object Value {
            get {
                return (ControlMode == SPControlMode.New 
                            || ControlMode == SPControlMode.Edit)
                        ? _editor.Text : ListItemFieldValue;
            }
            set {
                base.ItemFieldValue = value;
            }
        }
    
        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            EnsureChildControls();
        }
    
        protected override void CreateChildControls() {
            Controls.Clear();
            base.CreateChildControls();
    
            if (ControlMode == SPControlMode.New 
                    || ControlMode == SPControlMode.Edit) {
                _editor = new DropDownList();
                Controls.Add(_editor);
            } else {
                _litvalue = new Literal();
                _litvalue.Text = Convert.ToString(ListItemFieldValue);
                Controls.Add(_litvalue);
            }
        }
    }


    Page Layout Declaration:

    <controls:MyCustomFieldControl 
        id="MyCustomFieldControl1" 
        Runat="server" 
        FieldName="Field1" />
    
  • Create the field type class
    The field type class in the native implementation of the field behavior including the base type and the field rendering control. Here's an example:
    public class MyCustomField : SPFieldText {
        
        public MyCustomField(SPFieldCollection fields, string fieldName) 
            : base(fields, fieldName) {
        }
    
        public MyCustomField(SPFieldCollection fields, string fieldName, string displayName) 
            : base(fields, fieldName, displayName) {
        }
    
        public override BaseFieldControl FieldRenderingControl {
            get {
                BaseFieldControl fldControl = new MyCustomFieldControl();
                fldControl.FieldName = InternalName;
                return fldControl;
            }
        }
    }


  • Register the field type class
    Once the field type class is implemented and the assembly is deployed to your web frontend, you will need to register this new field type with the SharePoint runtime. To do this you need to add an xml configuration file to the 12/Template/XML directory named fldtypes_<custom name>.xml where <custom name> is a unique name associated with your customization. You can also view the default SharePoint field types registration XML configuration files in this directory.
    <FieldType>
        <Field Name="TypeName">My Custom Field</Field>
        <Field Name="ParentType">Text</Field>
        <Field Name="TypeDisplayName">My Custom Field</Field>
        <Field Name="TypeShortDescription">My Custom Field</Field>
        <Field Name="FieldTypeClass">MyCustomField, MyCustomFieldAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1df43608d339111</Field>
        <Field Name="UserCreatable">TRUE</Field>
    </FieldType>
    


Now that the field type is registered with SharePoint, you should now be able to create a new site column or list field based on this new custom field type.

Comments
No Comments Available
Add a New Comment
Name

Email Address

Url

Comment