v3 Creating Custom Field Types

Custom field types in SharePoint 2007 are pretty darn cool. They allow you to create your own subclasses of the existing SharePoint field types. You can control pretty much every aspect of the fields behavior. Things such as how the field  is displayed/rendered in the SharePoints interface, how the data is formatted when it gets stored in the field, validation, and all kinds of other cool stuff. 

So to try out custom field types I wanted to create a field for storing telephone numbers.

 

The field uses javascript to format the phone number automatically as the user types.  When the item is saved the custom field class strips out all the formatting characters that the javascript puts in and it also checks for proper length.  There are three main components to building a custom field…

·         Custom Field Class – Should inherit from an existing SPField class. Ie. SPFieldText

·         ASCX Control (Optional) – Defines a SharePoint:RenderingTemplate element whichs tells SharePoint how to render your control.

·         Custom Control Class(Optional) – The code-behind for your ascx control which defines how your control is rendered.

To get started open up vs 2005, create a new class library project, and add a reference to the Microsoft.SharePoint.dll.  Now create a new class file and name it TelephoneField.cs.  This will contain the code for our custom field class. Add the following using directives to the top of TelephoneField.cs…

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using Microsoft.SharePoint;

using System.Text.RegularExpressions;

Now copy and paste the following code into the body of the class (make sure to leave your namespace intact)…

View Code

As you can see our custom field class inherits from SPFieldText but you can inherit from pretty any other type of SharePoint field. This  class essentially controls what happens to the formatting of our fields data as it is retrieved from and inserted into the database.  You can see that in the GetValidatedString method we are also performing some validation and providing a message to the user if the data is incorrect.

Now add a text file to your solution and name it TelephoneFieldControl.ascx Copy and past the following into the TelephoneFieldControl.ascx file…

View Code

This  file is defining what the display of the field on the edit form will look like.  It contains javascript to perform the formatting of the text and also a textbox control named txtNumber. It would probly be best to put the javascript in an include file but for now we will just include it inline w/ the field.   

Now create another new class file and name it TelephoneFieldControl.cs.  This will contain the codebehind for the ascx file we created previously.  Add the following using directives to the top of the TelephoneFieldControl.cs file…

using System;

using System.Collections.Generic;

using System.Text;

using System.Web.UI.WebControls;

using Microsoft.SharePoint.WebControls;

Then add the following code to the body of the class…

View Code

The DefaultTemplateName property lets sharepoint know which rendering template to use this class for.  The value property controls how the data that SharePoint provides gets parsed into our control. The rest of the code is pretty standard as far as ascx controls go.

To finish the assembly project go ahead and create a key file and sign the assembly. It needs to be signed so we can install it into the GAC.

Before we can deploy the new field type we need to create a file called FLDTYPES_Telephone.xml. This is esentially a definition file that makes SharePoint aware of the new field type.  Copy and paste the follow into the FLDTYPES_Telephone.xml file (Make sure to replace the assembly name and public key token in the FieldTypeClass element with the correct values for your assembly)…

<?xml version="1.0" encoding="utf-8"?>

<FieldTypes>

  <FieldType>

    <Field Name="TypeName">PhoneNumber</Field>

    <Field Name="ParentType">Text</Field>

    <Field Name="TypeDisplayName">Phone Number</Field>

    <Field Name="TypeShortDescription">Phone Number</Field>

    <Field Name="UserCreatable">TRUE</Field>

    <Field Name="ShowInListCreate">TRUE</Field>

    <Field Name="ShowInSurveyCreate">TRUE</Field>

    <Field Name="ShowInDocumentLibraryCreate">TRUE</Field>

    <Field Name="ShowInColumnTemplateCreate">TRUE</Field>

    <Field Name="FieldTypeClass">TelephoneFieldType.TelephoneField,TelephoneFieldType,Version=1.0.0.0,Culture=neutral,PublicKeyToken=722d1e996cfca5d7</Field>

    <!-- We will implement this later to add display formating for the fields value. ie (xxx)xxx-xxxx

    <RenderPattern Name="DisplayPattern">

      <Switch>

        <Expr>

          <Column />

        </Expr>

        <Case Value="" />

        <Default>

          <HTML><![CDATA[^]]></HTML>

          <Column HTMLEncode="TRUE" />

        </Default>

      </Switch>

    </RenderPattern>

    -->

  </FieldType>

</FieldTypes>

I’ve commented out the render pattern section but it basically allows you to control how the field data is rendered in standard list views, headers, ect. w/ the use of CAML expressions. Its pretty powerful. There is more detailed info about render patterns in the wss3 SDK.

Deploying the custom field

 

1.       Build your assembly and install it in the GAC on the server.

2.       Copy the ascx control to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES

3.       Copy the FLDTYPES_Telephone.xml to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML

4.       Reset IIS

Now when you add fields to a list you should have to option of choosing Phone Number as a field type. Go try it out!

Resources used to write this post:

- Mark

?> kick it on SharePointKicks.com ?>

posted @ Monday, August 07, 2006 10:58 AM

Print