15 Sep 2011

Develop Custom Fields in SharePoint

Useful extensibility areas of Microsoft SharePoint 2010 is its capability to create custom fields to extend the native set of data types.

Custom Field must inherit from either SPField or from one of the classes that inherits from SPField

Custom field is made up of a set of classes, Important classes are:

Field Type The main class that defines the field type. This class is mandatory.

Field Rendering Control This control renders the field in a browser. It acts almost like any common ASP.NET server control. This class is optional, but often needed.

Field Mobile Rendering Control This control renders the field in mobile devices. It is optional.

Field Value Type This defines the class that holds the value of each instance of the custom field type, such as the field rendering control. While not mandatory, this class is very useful and commonly included.

Field Editor Type/Control Defines the control that allows advanced users to configure instances of the custom field type. This class is optional.

Field Type XML definition file reside in the  SharePoint14_Root\TEMPLATE\XML
File must be named using the structure FLDTYPES_*.XML, where the asterisk (*) is replaced with Title.

SPField Class


Main public and protected members of the SPField class.

public class SPField {
// Properties to define the custom field set of names
public string InternalName { get; }
public string StaticName { get; set; }
public string Title { get; set; }
// Methods to handle field value
public virtual object GetFieldValue(string value);
public virtual string GetFieldValueAsHtml(object value);
public virtual string GetFieldValueAsText(object value);
public virtual string GetFieldValueForEdit(object value);
public virtual string GetValidatedString(object value);
public virtual void ParseAndSetValue(SPListItem item, string value);
// Properties to define custom field related rendering controls
public virtual BaseFieldControl FieldRenderingControl { get; }
public virtual SPMobileBaseFieldControl FieldRenderingMobileControl { get; }
public virtual Type FieldValueType { get; }
// Methods to handle custom field configuration properties
public object GetCustomProperty(string propertyName);
public void SetCustomProperty(string propertyName, object propertyValue);
// Events handlers to track and manage
// field adding/deleting/updating tasks
public virtual void OnAddingToContentType(SPContentType contentType);
public virtual void OnAdded(SPAddFieldOptions op);
public virtual void OnDeleting();
public virtual void OnDeletingFromContentType(SPContentType contentType);
public virtual void OnUpdated();
// Methods to track and manage field adding/deleting/updating tasks
public virtual void Update();
public void Update(bool pushChangesToLists);
// Properties to define the custom field behavior in rendering UI
public bool? ShowInDisplayForm { get; set; }
public bool? ShowInEditForm { get; set; }
public bool? ShowInListSettings { get; set; }
public bool? ShowInNewForm { get; set; }
public bool? ShowInViewForms { get; set; }
public bool ShowInVersionHistory { get; set; }
// Properties to define the custom field behavior in rendering UI
public bool Required { get; set; }
public bool EnforceUniqueValues { get; set; }
public bool ReadOnlyField { get; set; }
public virtual bool NoCrawl { get; set; }

The Init method provides access to the configured values inside the custom field code

Example : Custom Field for regular expression validation





Example 2: International Bank Account Number – Custom Field

Add / Edit Mode:



Display mode.


Custom Field Rendering Templates

BaseFieldControl base class internally inherits from the SharePoint TemplateBasedControl
class, which holds a reference to a set of ITemplate instances.


Field Rendering Patterns

Defines column header rendering on list view pages.

Defines field rendering for the View (list item) form and for list view pages.

Defines field rendering for the Edit (list item) form.

Defines field rendering for the New (list item) form.

Defines a preview of how the field will look in Display mode, available for tools such as Microsoft Office SharePoint Designer 2010.

Defines a preview of how the field will look in Edit mode, available for tools such as Microsoft Office SharePoint Designer 2010.

Defines a preview of how the field will look in New mode, available for
tools such as Microsoft Office SharePoint Designer 2010.

Complete reference of CAML elements defined in the “View Schema,”:

Field Rendering Using XSLT

Starting with SharePoint 2010, every field has a rendering based on XSLT by default

A custom XSLT file for rendering a field:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="FieldRef[@Name='DevBANField']" mode="Note_body">
Value: <xsl:value-of select="translate(., ‘;#’, ‘ ‘)"
disable-output-escaping="yes" />


Custom Fields For Mobile Devices

To add mobile custom rendering, override the FieldRenderingMobileControl property of the SPField base class

Namespace for mobile:

You need to reference the System.Web.Mobile.dll assembly in your solution

Base class: SPMobileBaseFieldControl

Override the virtual methods for mobile rendering controls






System.Web.UI.MobileControls namespace provides all the controls for rendering on mobile:

For example:

Panel, LiteralText

Field Rendering Mobile Templates

Mobile rendering controls can also take advantage of ASCX templates similar to standard controls.

Available values for this set of List Template definitions

Location of template (ascx) file : SharePoint14_Root\TEMPLATE\CONTROLTEMPLATES folder.


Custom Field Editor

Whenever you create a new column instance in a list definition or define a new site column using a custom field type, you can also configure some custom information about the field instance.

Option1:  Basic: You can declare this information in the XML field definition file using the PropertySchema section of the FLDTYPES_*.XML

Option2:  Advance: Completely customizable field using ASCX control and code behind

To develop a custom field editor control, you need to define a field editor class and a field
editor ASCX. The class acts as the code-behind for the ASCX control. It must implement the
IFieldEditor interface



No comments:

Post a Comment