Wednesday, March 6, 2013

Creating a simple Custom Field Type for SharePoint 2010 using VS-2010


Email Validation Field
1.      In Visual Studio, create an Empty SharePoint Project. Give a suitable name for your project and here its “CustomNewFileld 
2.      Right-click the project name in Solution Explorer and select Add | New Item
3.      In the Add New Item dialog box, select Visual C#
4.      Select Class from the template and give name as EmailField.cs
5.      Coding EmailField.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;
namespace CustomNewFileld
{
    public class EmailField : SPFieldText // Base Class
    {
        public EmailField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        { // Default Constructor
        }
        public EmailField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        { // Default Constructor
        }
       // If you need default value in NewForm you can use the below method
       /*public override string DefaultValue
        {            get{ return "Test"; }        }*/

·         GetValidatedString is used for data serialization logic and for field validation logic that is specific to a custom field type to convert the field value object into a validated, serialized string.
·         GetValidatedString method check whether the field is required and, if it is the overriddenmethod throws an SPFieldValidationException exception when the value is null or an empty String.
·         SPFieldValidationException when the value is not valid, causing an error message to appear beneath (under) the invalid field.
        public override string GetValidatedString(object value)
        {
            String Email = (String)value;
            bool result = isEmailID(Email);
            if (Email == "")
                throw new SPFieldValidationException("Field should not be empty");
            else if (result != true)
            {
                throw new SPFieldValidationException("Enter valid Email");
            }
            else
            {
                return value.ToString();
            }
           
        }
        public static bool isEmailID(string inputEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
                return (true);
            else
                return (false);
        }
    }
}
6.      In Solution Explorer, right-click the project name and select Add, then SharePoint Mapped Folder 
7.      Use the tree control that opens to map the folder to TEMPLATE\XML and click OK.
8.      Right-click the new XML folder (not the project name) in Solution Explorer and select Add | New Item.
9.      In the Add New Item dialog box, select Visual C# and select an XML file; give the name as Fldtypes_EmailField.xml.

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
       <FieldType>
              <Field Name="TypeName">Email</Field>
              <Field Name="ParentType">Text</Field>
              <Field Name="TypeDisplayName">Email</Field>
              <Field Name="TypeShortDescription">Email Validation</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">          CustomNewFileld.EmailField,$SharePoint.Project.AssemblyFullName$</Field>
              </FieldType>
</FieldTypes>

CustomNewFileld - Namespace name (Mostly the project name).
EmailField .cs Filename which has the default constructors.
Email Validation – Name to be displayed in creating a New Column.
10. Right-click the References node in Solution Explorer, click Add Reference, and select PresentationFramework.dll on the .NET tab in the Add Reference dialog box. Click OK.
11. Finally Build and Deploy solution.

No comments:

Post a Comment