Creating Custom SharePoint 2010 Field Types
Summary: Learn how custom field types provide a powerful extensibility point for creating business solutions for Microsoft SharePoint 2010. Learn how to make a more polished column type by using custom logic to create default values and validate user input.
Last modified: September 12, 2012
Applies to: SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010
Provided by: Ted Pattison, Critical Path Training, LLC (SharePoint MVP)
Although site columns provide users and developers with new capabilities in reuse, you can define a reusable column definition that is even more powerful. With Microsoft SharePoint Foundation 2010, you can drop down to a lower level by creating custom field types.
The procedure that is demonstrated in this Microsoft SharePoint Visual How To provides a simple example of how to create a custom field type for a product code. The following are the high-level steps that are required to create a custom field type. To create a custom field control
Creating the Custom Field Class You must define the custom field class as public, and it must provide two nondefault constructors. This example also demonstrates how to validate field values using a regular expression by overriding the GetValidatedString method. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; namespace WingtipCustomFields { public class ProductCode : SPFieldText { public ProductCode(SPFieldCollection fields, string fName) : base(fields, fName) { } public ProductCode(SPFieldCollection fields, string tName, string dName) : base(fields, tName, dName) { } public override string DefaultValue { get { return "P001"; } } public override string GetValidatedString(object value) { if (!value.ToString().StartsWith("P")) { throw new SPFieldValidationException( "Product code must start with 'P'"); } if (value.ToString().Length != 4) { throw new SPFieldValidationException( "Product code must be 4 chars"); } // Always convert to uppercase before writing to Content DB. return value.ToString().ToUpper(); } } } Creating the Field Type Deployment File The Field Type Deployment file contains a Collaborative Application Markup Language (CAML) definition of the custom field type. You must name this file following the pattern of fieldtypes*.xml and then deploy it in the 14\TEMPLATE\XML directory. The file in this example is named fldtypes_WingtipCustomFields.xml.
<FieldTypes>
<FieldType>
<Field Name="TypeName">ProductCode</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">Product Code</Field>
<Field Name="TypeShortDescription">Wingtip Product Code</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">
WingtipCustomFields.ProductCode,
$SharePoint.Project.AssemblyFullName$
</Field>
</FieldType>
</FieldTypes>
![]() When you learn how to create a custom field type, you have the most control that SharePoint Foundation 2010 offers for creating a polished user interface for users to display and edit column values. Developing custom field types also provides you with a powerful new way to perform data validation before allowing user input values to be written to the content database. |
Watch the video
Length: 00:16:44
About the Author
|
Note
Ted Pattison is an author, instructor, and co-founder of