Thursday 30 May 2013

Creating Custom SharePoint 2010 Field Types

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.

Code It
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

  1. Create a public custom field type class, which inherits from one of the built-in field type classes, such as SPFieldBoolen, SPFieldChoice, or SPFieldText.
  2. Add two public constructors using specific parameter list signatures and forward parameters to base class constructors with matching signatures.
  3. Create an XML file known as the field type deployment file. You must deploy it in a well-known directory that activates the custom field type on a farm-wide basis.
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.
noteNote:
The SharePoint development tools in Microsoft Visual Studio 2010 let you use the $SharePoint.Project.AssemblyFullName$ token in place of the actual assembly name. The SharePoint tools replace this token with the assembly name when you compile your source files into a solution package.
<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>

Read It
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.

No comments:

Post a Comment