0

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?

1

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

1

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.

  • Please mark it as answer if it solves your query – Varun Verma Jul 9 '14 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.