Pages

Search This Blog

Monday, November 14, 2011

Creating Custom Field Types


In this post I will show how to develop a custom field type and try to add some custom properties which can be useful while creating it.

  • · Create an empty SharePoint project as the Microsoft Visual Studio does not provide the template for the custom Field type.
  • · For creating the field type you need to create a separate public class for each custom field type you want to add, you also need to add the XML definition file for deploying it.
  • · For each custom field type a separate public class needs to be created which gets inherited from one of the built in field types. Such as SPFieldText, SPFieldNumber, SPFieldDateTime, and SPFieldMultiColumn
Public class EmployeeStartDate: SPFieldDateTime
{
// custom field type implementation
}
Public class SocialSecurityNumber: SPFieldText
{
// custom field type implementation
}
Public class UnitedStatesAddress: SPFieldMultiColumn
{
// custom field type implementation
}
Public class EmployeeStatus: SPFieldText
{
// custom field type implementation
}
  • · The first step is to create two public constructors that are required by SharePoint in implementing the custom field type.
Public class EmployeeStartDate: SPFieldDateTime
{
Public EmployeeStartDate (SPFieldCollection fields, string fieldName)
: base (fields, fieldName) { }
Public EmployeeStartDate (SPFieldCollection fields, string typeName, string displayName)
: base (fields, typeName, displayName) { }
}
  • · Once you have added these two public constructors, the next step is to override whatever base class methods and properties make sense for your particular scenario. We will begin by examining the sample field type class named EmployeeStartDate, which has overridden the base members named OnAdded, Default Value and GetValidatedString.
Public class FieldEmployeeStartDate: SPFieldDateTime
{
// configure new fields to display date but not time
public override void OnAdded(SPAddFieldOptions op) {}
// add logic to create default date as first Monday
public override string DefaultValue { get {} }
// add validation to ensure start date is a Monday
public override string GetValidatedString(object value) {}
}
  • · Now, let us make the implementation of the OnAdded method. This method acts like an event handler which executes when the field type is added to any list.
  • · The implementation of OnAdded in the EmployeeStartDate class updates the DisplayFormat property of each new field to display its date value without the time.
public override void OnAdded(SPAddFieldOptions op)
{
this.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
this.Update();
}
  • · Let us have a look at, how to set the default value of a custom field type.
  • · Let’s make the default value of FieldEmployeeStartDate be Monday.
public override string DefaultValue
{
get
{
DateTime startDate = DateTime.Today;
// move forward to first Monday
while (startDate.DayOfWeek != DayOfWeek.Monday)
{
startDate = startDate.AddDays(1);
}
return SPUtility.CreateISO8601DateTimeFromSystemDateTime(startDate);
}
}

  • · Now let’s have a look at the implementation of validating the user inputs.
// add validation to ensure start date is a Monday
public override string GetValidatedString(object value)
{
DateTime input = System.Convert.ToDateTime(value);
if (input.DayOfWeek != DayOfWeek.Monday)
{
throw new SPFieldValidationException("Employee start date must be a Monday");
}
return base.GetValidatedString(value);
}
  • · Validation can also be made using the regular expression.
  • · Since the code writing is over, we need to deploy the custom field type, for this we need to write the XML with the name starting with the fldtypes and ends with the expression .xml, example: fldtypes_customTest.xml.
  • · There is a restriction when deploying the custom field type, i.e.. we need to deploy this xml in the /TEMPLATE/XML folder of the SharePoint 14 hive, and we cannot create a subfolder in the XML folder for deploying .
  • · For writing the XML we need to have some specific tags
<FieldTypes>
<FieldType>
<Field Name="TypeName"><!- Field Value -->Field>
<Field Name="ParentType"><!- Field Value -->Field>
<Field Name="TypeDisplayName"><!- Field Value -->Field>
<Field Name="TypeShortDescription"><!- Field Value -->Field>
<Field Name="UserCreatable"><!- Field Value -->Field>
<Field Name="ShowInListCreate"><!- Field Value -->Field>
<Field Name="ShowInDocumentLibraryCreate"><!- Field Value -->Field>
<Field Name="ShowInColumnTemplateCreate"><!- Field Value -->Field>
<Field Name="FieldTypeClass"><!- Field Value -->Field>
FieldType>
FieldTypes>

No comments:

Post a Comment