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
Usage
EmailField.cs
This acts as Field class for custom field type
EmailFieldControl.ascx
This is used to render the field in list’s Edit Form. Contains the rendering template.
EmailFieldControl.cs
This acts as Field Control class
Fldtypes_EmailField.xml
This contains the xml field definition
fldtypes_EmailField.xsl
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" ?>
<FieldTypes>
  <FieldType>
    <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>
  </FieldType>
</FieldTypes>

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

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
                xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
                version="1.0"
                exclude-result-prefixes="xsl msxsl ddwrt"
                xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
                xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
                xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:SharePoint="Microsoft.SharePoint.WebControls"
                xmlns:ddwrt2="urn:frontpage:internal">
  <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]))" />
      </xsl:variable>
    <span style="background:RGB(255,255,128)">
      <a target="_blank">
        <xsl:attribute name="href" >
          <xsl:value-of select="$MailToFldVal"/>
        </xsl:attribute>
        <xsl:value-of select="$thisNode/@*[name()=current()/@Name]" />
      </a>
    </span>
  </xsl:template >
</xsl:stylesheet>


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.

2 comments:

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

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

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

    ReplyDelete