WSS Dev: HowTo make a Custom FieldType

Draft…

If you want a column in a WSS list, where you either want custom validation or database filled dropdownlist then a Custom FieldType migth be the solution.

The steps involved can be:

  1. Create a new Custom FieldType
  2. Optional Add values to the new Custom FieldType
  3. Optional Add validation to the new Custom FieldType
  4. Optional Add FieldType properties
  5. Optional Promote the FieldType to a SiteColumn
  6. Optional Add the SiteColumn to a ContentType

1. Create a new Custom FieldType

  • Visual Studio: New Proj – WSPBuilder – Name Rasortypes
  • New Item – Custom Field Type – Name DirectionFieldType
  • Build and Test Deploy the new FieldType (Copy to 12hive, Copy to GAC)
  • Add the new FieldType to a test list in a test site
    • A test list:
    • Create a new column from the new FieldType:
    • Select the new FieldType:
    • Add an Item to the list:

      Notice that you cannot enter anything to the new column, yet. Code has to be entered first.
    • View the new item in the list:

2. Add values to the new Custom FieldType

  • Add possible input values to the edit control in DirectionFieldTypeControl.cs.
    Notice that the control is a ListBox in the case of this template. Often you would change it to a DropDownList!

            //DirectionFieldTypeControl.cs
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
                listBox = new ListBox();
                //Add your values here - could come from a DB
                listBox.Items.AddRange(
                    new ListItem[] {
                        new ListItem(String.Empty, null),
                        new ListItem("Left", "L"),
                        new ListItem("Right", "R"),
                        new ListItem("Straight ahead", "S"),
                        new ListItem("Back", "B")
                    }
                    );
    
                //Select the stored field value
                if (this.ItemFieldValue != null)
                {
                    ListItem li = listBox.Items.FindByValue(ItemFieldValue.ToString());
                    if (li != null)
                    {
                        li.Selected = true;
                    }
                }
                this.Controls.Add(listBox);
            }
            //read/write the value of this edit control from the field value
            public override object Value
            {
                get
                {
                    return this.ItemFieldValue;
                }
                set
                {
                    this.ItemFieldValue = value;
                }
            }
    
  • After yet a Copy to GAC, you’ll get this view:
  • For the list view mode change some CAML in fldtypes_DirectionFieldType.xml:
        <RenderPattern Name="DisplayPattern">
      <!--
          <HTML><![CDATA[TODO: define render pattern here]]></HTML>
        -->
      <HTML>
       <Column HTMLEncode="TRUE"/>
      </HTML>
        </RenderPattern>
    
  • Review the value stored in the column:

3. Add validation to the new Custom FieldType

To be added later.

4. Add FieldType properties

This could be a custom error message in case the validation failed.

To be added later.

5. Promote the FieldType to a SiteColumn through a feature

If we want to add the column to a ContentType then we need to promote the FieldType to a SiteColumn:

  • Visual Studio: New Item – Blank Feature – Name RoutingPlanFeat
  • Add <Field> element (a Site Column) to elements.xml, that links to <FieldType> XML\fldtypes_DirectionFieldType.xml
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <!--
    	<Field //A new Site Column
    	  ID="{8B071C81-F542-4ee6-A02C-6101E513381E}" //Create new GUID
    	  SourceID="http://schemas.microsoft.com/sharepoint/v3"
    	  Name="Direction"
    	  StaticName="Direction"
    	  DisplayName="Direction"
    	  Type="DirectionFieldType" //Link to fldtypes_DirectionFieldType.xml <FieldType/Field Name="TypeName">
    	  Group="Rasor Columns">
    	</Field>
    -->
    	<Field
    	  ID="{8B071C81-F542-4ee6-A02C-6101E513381E}"
    	  SourceID="http://schemas.microsoft.com/sharepoint/v3"
    	  Name="Direction"
    	  StaticName="Direction"
    	  DisplayName="Direction"
    	  Type="DirectionFieldType"
    	  Group="Rasor Columns">
    	</Field>
    </Elements>
  • WSPBuild and Test Deploy feature:
    • Build WSP, Deploy:
    • Activate Feature:
  • Verify existance: Browse to the Site Column


  • Add Site Column to a test list in a test site

6. Add the SiteColumn to a ContentType

To be added later.

The end

Advertisements

3 Comments

Add yours →

  1. I am working on a custom field type at the moment. And I have problem to create a site column with my custom field type. I can succesfully create a column in the custom list. But when I used the site interface to create a site column, i got unknown error.
    With your method (a feature), the site column is created and can be used in a custom list. But still when I clicked the site column in the ‘Site column gallery’ interface, I got the same message ‘Unknown error’.
    I want to know if you have any idea about it?

    • Hi Shansd

      I have one guess:
      – Is the feature of scope sitec ollection (should be)?

      BR Rasor

      • Yes, it’s a feature of scope ‘Site collection’. But I think the problme is not concerned with the feature. Because I have the same unknown error when I create a sie column in Sharepoint interface.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: