I am using sharepoint 2010. I would like to create a custom field type in Visual Studio which is inherited the "Text" field type. I never created a custom field type. Can someone tell me how to do this?
2 Answers
You can create your custom field with Visual studio which is inherited from SPFieldText.
For sample code:
public class EmailField : SPFieldText
{
public EmailField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public EmailField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
}
public override BaseFieldControl FieldRenderingControl
{
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
get
{
BaseFieldControl fieldControl = new EmailFieldControl();
fieldControl.FieldName = this.InternalName;
return fieldControl;
}
}
public override string GetValidatedString(object value)
{
if ((this.Required == true) && ((value == null)|| ((String)value == "")))
{
throw new SPFieldValidationException(this.Title + " must have a value.");
}
else
{
Email10ValidationRule rule = new Email10ValidationRule();
ValidationResult result = rule.Validate(value, CultureInfo.InvariantCulture);
if (!result.IsValid)
{
throw new SPFieldValidationException((String)result.ErrorContent);
}
else
{
return base.GetValidatedString(value);
}
}
}
}
}
You can find all steps mentioned below:
http://weblogs.asp.net/sreejukg/developing-custom-field-type-for-sharepoint-2010
You need to override the SPFieldText Class.
Add the required constructors.
public ISBNField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public ISBNField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
}
Now you need to override the class in which you are providing your field's functionality.