Share via


SharePoint 2013: Programmatically creating a field from the Custom field type

Requirement: Programmatically create a field/column from a custom field type.

Following is a simple example with sample code to create a field from custom field type. In this example we are creating a custom field type (derived from SPFieldNumber) and add it to document library.

Note: I would recommend to refer to this MSDN post before this post to understand the entire steps involved to create a custom field type.

/// <summary>
 /// Create custom field as XML
 /// </summary>
 /// <param name="oId">Field GUID</param>
 /// <param name="strDisplayName">Unique Display Name of field</param>
 /// <param name="Required">Field required property</param>
 /// <param name="strFieldType">Custom field type</param>
 /// <param name="strFieldName">Custom field display name</param>
 /// <returns>string</returns>
 internal static  string GetCreateFieldAsXml(Guid oId, string strDisplayName, bool Required, string strFieldType, string strFieldName)
 {
 XmlElement element = new  XmlDocument().CreateElement("Field"); // Creating the "Field" element for the Inner XML schema
 element.SetAttribute("ID", oId.ToString()); // Setting the GUID of the field from value passed
 element.SetAttribute("Type", strFieldType); // Setting the Parent Type name of custom type registered in the "Fldtypes*.xml" file
 element.SetAttribute("Name", strFieldName); // Setting the Type Display Name registered in the "Fldtypes*.xml" file
 element.SetAttribute("DisplayName", strDisplayName); // Any unique Display Name 
 element.SetAttribute("Required", Required.ToString().ToUpper());
 return element.OuterXml; // Returning the OuterXML to create the field as XML
 }

**Creating the Custom Field Class **

The following example defines a custom control for displaying the custom field of type SPFieldNumber. The example overrides the OnAdded method to contain the logic while adding the custom field type in library/list.

using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 namespace Prasath.SP
{
 class SampleHitCountField: SPFieldNumber
 {
 public SampleHitCountField(SPFieldCollection fields, string fieldName)
 : base(fields, fieldName)
 {
 }
 
 
 public SampleHitCountField(SPFieldCollection fields, string typeName, string displayName)
 : base(fields, typeName, displayName)
 {
 }
 
 
 //Event triggered when new field is added to the document library
 public override  void OnAdded(SPAddFieldOptions op)
 {
 base.OnAdded(op);
 Update();
 
 
 //Hide the field from new and edit form
 this.ShowInEditForm = false;
 this.ShowInNewForm = false;
 this.DefaultValue = "0";
 this.EnforceUniqueValues = false;
 this.Required = false;
 this.Update();
 }
 }
}

Creating the Field Type Definition

The following example registers the custom field type. This file must be named in the format fldtypes*.xml

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
 <FieldType>
 <Field Name="TypeName">SampleHitCountField</Field>
 <Field Name="TypeDisplayName">Total View Count</Field>
 <Field Name="InternalType">SampleHitCountField</Field>
 <Field Name="TypeShortDescription">Total View Count</Field>
 <Field Name="FieldTypeClass">Prasath.SP.SampleHitCountField, $SharePoint.Project.AssemblyFullName$</Field>
 <Field Name="ParentType">Number</Field>
 <Field Name="Sortable">TRUE</Field>
 <Field Name="Filterable">TRUE</Field>
 <Field Name="UserCreatable">TRUE</Field>
 <Field Name="ShowOnListCreate">FALSE</Field>
 <Field Name="ShowOnSurveyCreate">FALSE</Field>
 <Field Name="ShowOnDocumentLibrary">TRUE</Field>
 <Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
 <Field Name="ShowInListSettings">TRUE</Field>
 </FieldType>
</FieldTypes>

Creating a Field from custom Field Type

Following is the method to create field from custom type. Code to custom field as XML from custom field type:

String strFieldasXML = GetCreateFieldAsXml(Guid.NewGuid(), "Hit Count Field", false, "SampleHitCountField", "SampleHitCountField");
web.Fields.AddFieldAsXml(strFieldasXML); //here 'web' is a SPWeb object