Create Custom Field Type in SharePoint

Just follow the below steps to create a custom field type

Its very simple to create a custom field type in SharePoint


Step 1  : Open VS and create a new SharePoint Project

Step 2:  Add SharePoint mapped folder
   
              - Control Templates

Step 3 : Add another SharePoint Mapped folder 

              - XML

Step 4: Then create user control named "MyField.ascx" and delete the code              file of the user control
            
            In our ascx file, delete the links to the code behind class and finally it                should be 
               
                <%@ Control Language="C#" %>

Step 5 : Add the below line in the ascx file 

            <SharePoint:RenderingTemplate ID="MyRenderingTemplate" runat="server">
            <Template>
                 <asp:TextBox ID="txtMyField" runat="server" CssClass="ms-long" />
            </Template>
         </SharePoint:RenderingTemplate>

Step 6: Add MyField.cs class file 


    public class MyField : SPFieldText
    {
        public MyField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {

        }
        public MyField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
        }

        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl fieldControl = new EmailIDFieldControl();
                fieldControl.FieldName = this.InternalName;
                return fieldControl;
            }
        }


 }


Step 7: Add MyFieldControl.cs class file

public class MyFieldControl : BaseFieldControl
    {
        protected TextBox txtMyTextBox;
        protected override string DefaultTemplateName
        {
            get
            {
                return "MyRenderingTemplate";
            }
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            txtMyTextBox = (TextBox)this.TemplateContainer.FindControl("txtMyField");
            try
            {
                if (this.ItemId <= 0)
                {
                    txtMyTextBox.Text = "";                   
                }

            }
            catch (Exception e)
            {

            }
        }
        public override object Value
        {
            get
            {
                this.EnsureChildControls();
                return txtMyTextBox.Text;
            }
            set
            {
                this.EnsureChildControls();
                txtMyTextBox.Text = (string)this.ItemFieldValue;
            }
        }
    }




Step 8: Add XML file schema for the field type (this should be created under folder named XML (which is SharePoint mapped folder)

<FieldTypes>
  <FieldType>
    <Field Name="TypeName">MyField</Field>
    <Field Name="ParentType">Text</Field>
    <Field Name="TypeDisplayName">My Custom Field</Field>
    <Field Name="TypeShortDescription"> My Custom Field (My Custom Field for user)</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="ShowInListCreate">TRUE</Field>
    <Field Name="ShowOnDocumentLibraryCreate">FALSE</Field>
    <Field Name="ShowInSurveyCreate">TRUE</Field>
    <Field Name="ShowInColumnTemplateCreate">TRUE</Field>
    <Field Name="FieldTypeClass">YourSolutionName,$SharePoint.Project.AssemblyFullName$</Field>
  </FieldType>
</FieldTypes>


Note that the xml file name must begin with fldtypes_

Then deploy the solution.


I believe this will helps you....






Comments

Popular posts from this blog

IRM and the Object Model

This content database has a schema version which is not supported in this farm

Activate and Deactivate Feature through PowerShell