JIemON
JIemON

Reputation: 397

How to add standart settings from choice field in new custom field based on Choice Type

I want to create custom field type based on Choice field in SharePoint 2010. My code: Fldtypes_OpenChoiceField.xml:

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">OpenChoice</Field>
    <Field Name="ParentType">Choice</Field>
    <Field Name="TypeDisplayName">OpenChoice</Field>
    <Field Name="TypeShortDescription">Choice with open answers</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="AllowBaseTyp eRendering">TRUE</Field>
    <Field Name="ShowOnListAuthoringPages">TRUE</Field>
    <Field Name="ShowOnDocumentLibraryAuthoringPages">TRUE</Field>
    <Field Name="ShowOnSurveyAuthoringPages">TRUE</Field>
    <Field Name="ShowOnSurveyCreate">TRUE</Field>
    <Field Name="ShowOnColumnTemplateAuthoringPages">TRUE</Field>
    <Field Name="FieldTypeClass">FieldTypes.SharePoint.OpenChoiceField,$SharePoint.Project.AssemblyFullName$</Field>
    <Field Name=" ">/_controltemplates/Fields_SharePoint/OpenChoiceFieldEditor.ascx</Field>
  </FieldType>
</FieldTypes>

OpenChoiceField.cs:

namespace FieldTypes.SharePoint
{
    public class OpenChoiceField :  SPFieldChoice
    {
        public OpenChoiceField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
        }

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

When I create column with custom field type I see in section Additional Column Settings only default settings (description, require, enforce unique, add to default view). But I need all setting such in Choice Field (Type each choice..., Display choices using, Allow Fill-in choices, default value). How can I insert these properties in my custom field? May be need I add some standart controls to edit control (/_controltemplates/Fields_SharePoint/OpenChoiceFieldEditor.ascx)?

Upvotes: 0

Views: 1279

Answers (1)

skeletank
skeletank

Reputation: 2888

If you want a good example of a custom field then go to this blog by Bernado Nguyen-Hoan.

You will have to add the custom properties to your xml like such:

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">ImageUpload</Field>
    ...
    ...
    <PropertySchema>
      <Fields>
        <Field 
            Name="UploadImagesTo"
            DisplayName="UploadImagesTo"
            MaxLength="255"
            DisplaySize="100"
            Type="Text"
            Hidden="TRUE">
          <Default>Images</Default>
        </Field>
      </Fields>
    </PropertySchema>
  </FieldType>
</FieldTypes>

You will also have to override the Update method in your OpenChoiceFieldClass:

public override void Update()
{
  base.SetCustomProperty("UploadImagesTo",
  Thread.GetData(Thread.GetNamedDataSlot("UploadImagesTo")));

  base.Update();
}

The blog post will also show you how to set up your own custom Control and Editor for the field as well.

Upvotes: 1

Related Questions