Programmatically creating a field (column) from the Custom field type in WSS 3.0 – MOSS

Many of you might have programmatically created the fields in the list or Site (Site Columns) using the SharePoint object model. As long as you are creating the field based on the OOB field types then it’s just a cake walk like follows : (The code snippet shows the sample of Windows Application)

SPSite oSite = new SPSite(textBox1.Text);

            SPWeb oWeb = oSite.OpenWeb();

            SPList oList = oWeb.Lists[textBox3.Text];

            // Adding the Custom field to the List. Here the OOB SPFieldText has been selected as the “FieldType”

            string OfieldName = oList.Fields.Add("ProgramField",SPFieldType.Text,false); // There are three overloaded methods available on adding the field

            oList.Update();

            oWeb.Update();

            // End of adding field to the List

            // Adding the Custom Field to the Web as Site Column. Here the OOB SPFieldText has been selected as the “FieldType”

            string OSiteColumnName = oWeb.Fields.Add("ProgramSiteColumn", SPFieldType.Text, false);

            oWeb.Update();

            // End of adding field to the Site

Pretty Easy !!

Now consider that you have created a custom field type called “SPCustomFieldText” by inheriting the OOB “SPFieldText”. You have successfully registered it as a Custom Field type though the “FldTypes*.xml” file. You can find the article here that how can you create and register your custom field type in the SharePoint.

So the custom field type is successfully created and register in your SharePoint and now you want to create a field programmatically based on the custom field type using the above code snippet.

BOOOOOM !!!

Yes, the “SPFieldType” enumerator doesn’t consist your Custom field type name, It only consists the OOB field type name. UFFF !! So you cannot use the above shown code snippet to create fields from the custom field type.

Just a few glance in the SDK and hurray I found it. Yes, SharePoint provides another way (method) to create the fields using the XML format J where you can specify your custom field type name without using the SPFieldType enumerator.

The following code snippet shows that how to create fields from the custom field type using the “AddFieldAsXml()” method. The type and the name of the field type should be the name you registered in the custom “Fldtypes*.xml” file.

For your conveneint I have pasted the partial code snippet of the “FldTypes*.xml” file where you can find the entry of the Typename of your custom field type

 

<?xml version="1.0" encoding="utf-8"?>

<FieldTypes>

  <FieldType>

    <Field Name="TypeName">CustomFieldType</Field>

    <Field Name="ParentType">Text</Field>

    <Field Name="TypeDisplayName">Cascading Drop Down List</Field>

    <Field Name="TypeShortDescription">Cascading Drop Down List</Field>

    <Field Name="UserCreatable">TRUE</Field>

.

.

.

.

.

.

(For example, for the OOB SPFieldText (Single line of text) the type and the field name is “Text” which was registered in the OOB Fldtypes.xml file)

SPSite oSite = new SPSite(textBox1.Text);

            SPWeb oWeb = oSite.OpenWeb();

            SPList oList = oWeb.Lists[textBox3.Text];

            string strFieldasXML = GetCreateFieldAsXml(Guid.NewGuid(), "ProgrammaticField1", false, "CustomFieldType", "CustomFieldType");

            oList.Fields.AddFieldAsXml(strFieldasXML);

            oList.Update();

            oWeb.Update();

internal 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 Type name registered in the “Fldtypes*.xml” file

            element.SetAttribute("Name", strFieldName); // Setting the Field name registered in the “Fldtypes*.xml” file

            element.SetAttribute("DisplayName", strDisplayName); // Any unique Display Name

     element.SetAttribute("Required", Required.ToString());

            return element.OuterXml; // Returning the OuterXML to create the field as XML

        }

The only important thing you need to be carefull is on setting the “Type” and “Name” of the field type. Again I am iterating to make sure that you enter the “Type” and “Name” value as it is in the “FldTypes*.xml” file.

 

Hope it helps someone and save their time J