How to create a custom field type or field group

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 classMyCustomFieldControl:BaseFieldControl{privateDropDownList_editor;privateLiteral_litvalue;public override objectValue {get{return(ControlMode ==SPControlMode.New || ControlMode ==SPControlMode.Edit) ? _editor.Text : ListItemFieldValue; }set{base.ItemFieldValue =value; } }protected override voidOnInit(EventArgse) {base.OnInit(e); EnsureChildControls(); }protected override voidCreateChildControls() { Controls.Clear();base.CreateChildControls();if(ControlMode ==SPControlMode.New || ControlMode ==SPControlMode.Edit) { _editor =newDropDownList(); Controls.Add(_editor); }else{ _litvalue =newLiteral(); _litvalue.Text =Convert.ToString(ListItemFieldValue); Controls.Add(_litvalue); } } }

Page Layout Declaration:

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 classMyCustomField:SPFieldText{publicMyCustomField(SPFieldCollectionfields,stringfieldName) :base(fields, fieldName) { }publicMyCustomField(SPFieldCollectionfields,stringfieldName,stringdisplayName) :base(fields, fieldName, displayName) { }public overrideBaseFieldControlFieldRenderingControl {get{BaseFieldControlfldControl =newMyCustomFieldControl(); fldControl.FieldName = InternalName;returnfldControl; } } }

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_.xml where is a unique name associated with your customization. You can also view the default SharePoint field types registration XML configuration files in this directory.
My Custom Field Text My Custom Field My Custom Field MyCustomField, MyCustomFieldAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1df43608d339111 TRUE

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s