Creating Custom Field Type in Sharepoint List

Sharepoint comes with a great GUI tool that allows administrator to create and manage data structure, that means we could create our own columns and set up the associations, etc. However, the default field types out from the box are limited. The great news is we could create our own field types with own business logic. I just tried to create a custom field type called “Phone Number”, which is a standard text field but with Javascript validator that validates the input string to be a valid phone number.

The general process isn’t hard, but the steps to configure and integrate into Sharepoint services could be a bit complicated for first time Sharepointer like me. I am notting down the steps and hopefully it could be useful for anyone:

1. First thing is to create own field class e.g. TelephoneField: SPFieldText. It has to inherits the SPFieldText and overrides a list of methods. Give the class a proper namespace. Invalid namespaces and class names could make the integration work sucks.

2. Then, create the usercontrol TelephoneFieldControl.ascx, which is the interface of the text field. Must register the Sharepoint assembly at the top of the control:

(e.g. <% @Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@RegisterTagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, 
PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>)

Create your field in the SharePoint rendering template tag:

<SharePoint:RenderingTemplate ID=”TelephoneFieldControl” runat=”server”>
<Template> //DO YOUR STUFF HERE </Template>
</Sharepoint:RenderingTemplate>

3. In the code behind of the usercontrol, inherits BaseFieldControl and overrides the following methods:

  • string DefaultTemplateName
  • object Value
  • void Focus
  • void CreateChildControls

4. Create an XML file: (Check the name space in FieldTypeClass)

<?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=cabdaefbfc3fa942</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>

 

5. In properties of the project, go to Signing, make sure the “Sign the assembly” checkbox is checked and write in “GACkey.snk” into the “Choose a strong name key file” field.

6. Now we are ready to deploy. Go to the bin folder of the project and drag the dll into C:\Windows\Assembly. That will install the assembly into GAC, and then copy and paste the XML we created into C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML. Copy and paste the usercontrol file into C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES.

7. Restart IIS by going to command prompt and hit “iisreset”.

8. Test out in Sharepoint by creating a new column.

Categories: Sharepoint 2007
Tags: