Sunday, 18 March 2012

MP.SPDevExt VSIX for SharePoint 2010: Custom Field Type

MP.SPDevExt Blog Series:
Part 1: MP.SPDevExt VSIX for SharePoint 2010
Part 2: MP.SPDevExt VSIX for SharePoint 2010: Custom Field Type (Currently reading)
Part 3: MP.SPDevExt VSIX for SharePoint 2010: Sandbox Workflow Action
Part 4: MP.SPDevExt VSIX for SharePoint 2010 : Sandbox Solution Validator

With low on energy (fasting) and no intension to write…I started my readings today morning….while surfing I found two things which diverted my mind to write….

1.       My VS 2010 extension for SharePoint (MP.SPDevExt) got downloaded 30+ times in mere 2 days
2.       MS SharePointTeam Blog post mentioning about @ScotHillier tweet….

“Back in the day, products came with manuals. Now they come with communities.” @ScotHillier

True…so this being the case, this blog that I was thinking to write in next week, I took it in writing immediately so that people using/exploring my extension should not have any problems…

Please go through the Part 1 of this series if you have not.

This post of the series will demonstrate the Custom Field Type item template….Here are the steps...

1.       In Visual Studio 2010, click New Project, expand the SharePoint node, click 2010, and then click Empty SharePoint Project. Name the project as MP.SPDevExtDemo and then click OK.
2.       In the SharePoint Customization Wizard, select the local SharePoint site that can be used for debugging and  Deploy as a farm solution option shown in Figure 1. Click Finish.

Figure 1: Select Deployment Method

3.       Go to Solution Explorer and right click on MP.SPDevExtDemo Project and click on Add=>New Item

Figure 2: Add New Item

4.       In Add New Item window, select the SharePoint=>2010 tab in left menu. Then click on Custom Field Type (MP.SpDevExt) item template, type EmailField in Name textbox and click on Add button.

Figure 3: Add Custom Field Type item

5.       This will create EmailField SharePoint project item as shown in figure 4

Figure 4: EmailField project item

The below table shows the files generated along with their usages. By default Custom Field Type item template generates code having Email validation.

File Name
This acts as Field class for custom field type
This is used to render the field in list’s Edit Form. Contains the rendering template.
This acts as Field Control class
This contains the xml field definition
This contains the xslt used to render the field in list view forms.

6.       In Fldtypes_EmailField.xml file, copy the following xml.

<?xml version="1.0" encoding="utf-8" ?>
    <Field Name="TypeName">EmailField</Field>
    <Field Name="ParentType">Text</Field>
    <Field Name="TypeDisplayName">EmailField</Field>
    <Field Name="TypeShortDescription">Email Field (MP.SPDevExt)</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="ShowOnListCreate">TRUE</Field>
    <Field Name="ShowOnSurveyCreate">TRUE</Field>
    <Field Name="ShowOnDocumentLibraryCreate">TRUE</Field>
    <Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
    <Field Name="FieldTypeClass">MP.SPDevExtDemo.EmailField,$SharePoint.Project.AssemblyFullName$</Field>

7.       In fldtypes_EmailField.xsl, copy the following xsl.

<xsl:stylesheet xmlns:x=""
                exclude-result-prefixes="xsl msxsl ddwrt"
  <xsl:template match="FieldRef[@FieldType = 'EmailField']" mode="Text_body">
    <xsl:param name="thisNode" select="." />
      <xsl:variable name="MailToFldVal">
        <xsl:value-of select"concat('mailto:',string($thisNode/@*[name()=current()/@Name]))" />
    <span style="background:RGB(255,255,128)">
      <a target="_blank">
        <xsl:attribute name="href" >
          <xsl:value-of select="$MailToFldVal"/>
        <xsl:value-of select="$thisNode/@*[name()=current()/@Name]" />
  </xsl:template >

8.       Right click in MP.SPDevExtDemo project and click on Deploy menu as shown in figure 5. Visual studio will package and deploy the solution to the farm.

Figure 5: Deploy  solution

9.       Navigate to any local SharePoint site and create a list named Demo List using custom list template
10.   Go to the list settings page of Demo List.
11.   Click on Create Column link
12.   A New Email Field (MP.SPDevExt) field type will be available for the selection. Select the same. Name the column as Email and click Ok button as shown in figure 6.

Figure 6: Create Column using new field type

13.   Create the new item to test the custom field type column. As by default, Custom Field Type item template creates field type having email validation code built-in.

Figure 7: Email field with Textbox
Figure 8: Email validation message
Figure 9: Email Field in List view

You will find some commented code/methods which serve different purposes when uncommented. Kindly go through the summary added for such commented methods.

I remember when I wanted to create custom field, I needed to google for the files/code required and then manually add these files in code at appropriate places… Now with the Custom Field Type item template available, I believe some of time required for such activities will be saved and productivity will have positive impact.  

Ending this post with the hope that you will like the Custom Field Type item template. Your comments/ feedbacks are most welcome.


  1. Akhilesh Nirapure6/07/2012 12:35 pm

    Awesome Solution mate :-)
    i'll try this soon
    - Akhilesh

  2. Glad you liked it. Hopefully it proves handy. :-)