Tuesday, July 17, 2012

How to Create Custom Field Types?

We can create a custom field types using 2 ways
By selecting the custom field template in Visual Studio or we can develop as bellow

There are three main components to building a custom field

Custom Field Class
Should inherit from an existing SPField class. Ie. SPFieldText or SPFieldMultiColumn. The important property is override FieldRenderingControl in which it Creates an instance of the field control class and initialize its FieldName property with internal name property of the SPfield class.

In this we can override GetValidatedString() method to validate the input string like the field value is mandatory or other anything.

ASCX Control (Optional) –
Defines a SharePoint:RenderingTemplate element whichs tells SharePoint how to render your control.
Here we need to specify the assembly details.

Custom Control Class(Optional) –
The code-behind for your ascx control which defines how your control is rendered.
In this class we need to override two properties DefaultTemplateName where we need to Pass ID of RenderingTemplate control to WSS runtime which enables a connection between the control and code behind.
Value which Reads and writes the field's underlying value to and from the content database.
Override the CretaeChildControl method to initialize the custom field control using TemplateContainer which is a member of BaseFieldControl And contains the reference of the RenderTemplate control.
Like textbox=(Textbox) TemplateContainer.FindControl(“txtbox”);
See the Code snippet from Suman/Codesnippets/Customization/CustomField
SPFieldMultiColumn:
If we want to create a group of columns for ex: the Address which contains address, zip code, street etc… to implement this type of CFT we need to create a class which should be inherited by SPFieldMultiColumnValue class, which stores one property per item (like street, city …) in the structured value . This class is called as custom field value class.

Next steps are same as above simple CFTs, specifically for this type of CFTs we need to create a filed class by deriving the SPFieldMultiColumn class. Here the important one is we need to override GetFiledValue() method to return the serialized value stored in the database as specified type.

In custom control class, we need to do the same way as we have done in simple CFTs, just we need to construct the object of our type and pass the values to that object, and set the values to this object (Get/Set) in Value property.

The best ex:
public override object Value
{
get
{
EnsureChildControls();
FieldAddressValue fieldValue = new FieldAddressValue();
fieldValue.Address = addressBox.Text.Trim();
fieldValue.Zip = zipBox.Text.Trim();

return fieldValue;
}
set
{
EnsureChildControls();
FieldAddressValue fieldValue = (FieldAddressValue)value;
addressBox.Text = fieldValue.Address;
zipBox.Text = fieldValue.Zip;

}

Here in Value property we have created custom Filed value class object and reading the values from and to content database using get/set.
In this way, we can implement the Multi Column CFTs.
The final important one is, Rendering the multiple values in view list items page, this could be done by RenderPattern element of Fields.xml file as shown in bellow by using CAML query.

Suppose in our ex: we have 4 properties to display in list view, for that we can implement like this.



HTML>]]>

HTML>

HTML>]]>




SubColumnNumber specifies the properties which we were defined in a custom Field value Type class, we can render these properties in this way.
In Final step we have to create a Field type.xml (fldTypes.xml ) file in 12\templates \xml as bellow.

PhoneNumber
Text
Phone Number
Phone Number
TRUE
TRUE
TRUE
TRUE
TRUE
TelephoneFieldType.TelephoneField,TelephoneFieldType,Version=1.0.0.0,Culture=neutral,PublicKeyToken=722d1e996cfca5d7

For Multi field look at this blog

Another Good Blog for Custom Fields in MOSS

No comments:

Post a Comment