Question

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?

Was it helpful?

Solution

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://www.c-sharpcorner.com/uploadfile/Roji.Joy/creating-a-custom-field-type-for-sharepoint-2010-email-validation-field/

http://weblogs.asp.net/sreejukg/developing-custom-field-type-for-sharepoint-2010

OTHER TIPS

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.

Details could be found here.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top