Monday, May 27, 2013

Custom field type with visual studio 2010


Steps to create Custom field type with visual studio 2010

1) Create new project select "Empty SharePoint project"

2) Give project name "CustomFieldType"

3) Select deploy solution as a farm solution.

4) Select project and click on add "SharePoint mapped folder" go to template > xml




5) Select XML folder right click add > new item


6) Go to Data tab and select XML file. Give file name" fldtypes_CustomFieldTraining.xml " ,Be sure the xml file name should has to start with fldtypes only. Otherwise this will not work.


7) open the fldtypes_CustomFieldTraining.xml file and enter the below node.

<!--
This file ensures that your SPField class is displayed in the list of available field types -->
<
FieldTypes>
<FieldType>  
<Field Name="TypeName">TrainingCode</Field> 
<Field Name="ParentType">Text</Field>  
<Field Name="TypeDisplayName">Training Code</Field>  
<Field Name="TypeShortDescription">Training Code</Field>  
<Field Name="UserCreatable">TRUE</Field>  
<Field Name="FieldTypeClass">CustomFieldType.Training_CustomFieldType, $SharePoint.Project.AssemblyFullName$</Field>
</FieldType>
</FieldTypes>

8) Enter a class file and give the name : Training_CustomFieldType.cs
Please check the name correctly because this is used in xml file as FieldTypeClass.


9) put the below lines in the CS file.

using System;
using System.Collections.Generic;
using System.Linq;using System.Text;
using Microsoft.SharePoint;
namespace CustomFieldType
{

/// <summary>/// When a user creates a new column in a List or Content Type, they see a list of /// Field Types, such as "Single Line of Text" and "Date and Time". By creating your/// own Field Type, you can extend this list. You can also include your own logic in /// the new Field Type, such as custom code to set the default value or validate user/// input. This code example shows how to create a custom Field Type/// </summary>/// <remarks>/// To create a custom field type, create an empty SharePoint project and then add a /// new class. The new class should inherit from a SPField class. In this case, the /// field stores strings, so we inherit from SPFieldText. You must also create a /// fldtypes*.xml file and deploy it to TEMPLATES\XML in the 14 hive. class Training_CustomFieldType:SPFieldText{#region Constructors
//You must create both these constructors, passing the parameters to the base classpublic Training_CustomFieldType(SPFieldCollection fields, string fName):
base(fields, fName){
}

public Training_CustomFieldType(SPFieldCollection fields, string tName, string dName):
base(fields, tName, dName){
}
#endregion//Override this property to formulate a default value. public override string DefaultValue{

get{return "CP0001";}
}

//Override this method to validate string data.public override string GetValidatedString(object value){

//Check that it starts with CP for "Training Code"if (!value.ToString().StartsWith("MS")){

//When you throw an SPFieldValidationException users see red text in the UIthrow new SPFieldValidationException("Training code must start with 'MS'");}

//Check that it's 6 characters longif (value.ToString().Length != 6){

throw new SPFieldValidationException("Training code must be 6 characters long");}

//Always convert to uppercase before writing to Content DB.return value.ToString().ToUpper();}
}
}


10) Deploy the solution and check the custom field while creating column.




http://msdn.microsoft.com/en-us/library/vstudio/ee361616.aspx

No comments:

Post a Comment

HTML

Script:

JS