Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

From your experience, what are some of the best practices around custom field types, their creation, deployment and maintenance. I am finding little on this topic online.

share|improve this question

closed as not constructive by Kit Menke Jan 13 '12 at 14:38

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

    
I'm not sure what a content type field is. Do you mean content types or site columns? –  David Lozzi Jan 11 '12 at 20:04
    
Fields that you add to your content types are at site collection level known as site columns. When you create sp fields, do not use space or special characters. For example, Vendor Name should be created as VendorName. Today's Date should be created as TodaysDate. etc. –  Mike conway Jan 11 '12 at 20:53
3  
Please be more specific, or question will be closed. The better and more specific the question, the better chance of a good reply. Vague questions like the above wont give you the answers you are seeking, and keeps us guessing what you want to know. –  Anders Rask Jan 11 '12 at 21:20
    
Apologies, I cleared it up and was more specific. I meant custom field types. Again - I apologize. –  dbobrowski Jan 11 '12 at 22:07
2  
I found WHOLE bunch of good links google.com/… –  Mike conway Jan 11 '12 at 22:13

1 Answer 1

The following are the high-level steps that are required to create a custom field type.

To create a custom field control

Create a public custom field type class, which inherits from one of the built-in field type classes, such as SPFieldBoolen, SPFieldChoice, or SPFieldText. Add two public constructors using specific parameter list signatures and forward parameters to base class constructors with matching signatures. Create an XML file known as the field type deployment file. You must deploy it in a well-known directory that activates the custom field type on a farm-wide basis.

Creating the Custom Field Class You must define the custom field class as public, and it must provide two nondefault constructors. This example also demonstrates how to validate field values using a regular expression by overriding the GetValidatedString method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace WingtipCustomFields {
  public class ProductCode : SPFieldText {

    public ProductCode(SPFieldCollection fields, string fName)
      : base(fields, fName) { }
    public ProductCode(SPFieldCollection fields, 
                       string tName, string dName)
      : base(fields, tName, dName) { }

    public override string DefaultValue {
      get { return "P001"; }
    }

    public override string GetValidatedString(object value) {
      if (!value.ToString().StartsWith("P")) {
        throw new SPFieldValidationException(
                    "Product code must start with 'P'");
      }
      if (value.ToString().Length != 4) {
        throw new SPFieldValidationException(
                    "Product code must be 4 chars");
      }

      // Always convert to uppercase before writing to Content DB.
      return value.ToString().ToUpper();
    }
  }
}

Creating the Field Type Deployment File The Field Type Deployment file contains a Collaborative Application Markup Language (CAML) definition of the custom field type. You must name this file following the pattern of fieldtypes*.xml and then deploy it in the 14\TEMPLATE\XML directory. The file in this example is named fldtypes_WingtipCustomFields.xml.

Note: The SharePoint development tools in Microsoft Visual Studio 2010 let you use the $SharePoint.Project.AssemblyFullName$ token in place of the actual assembly name. The SharePoint tools replace this token with the assembly name when you compile your source files into a solution package.

XML

<FieldTypes>
  <FieldType>
    <Field Name="TypeName">ProductCode</Field>
    <Field Name="ParentType">Text</Field>
    <Field Name="TypeDisplayName">Product Code</Field>
    <Field Name="TypeShortDescription">Wingtip Product Code</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="FieldTypeClass">
      WingtipCustomFields.ProductCode,
      $SharePoint.Project.AssemblyFullName$
    </Field>
  </FieldType>
</FieldTypes>

For more information see this example

share|improve this answer

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