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
Comments
Anonymous
July 09, 2010
The comment has been removedAnonymous
October 19, 2010
Thanks a lot for sharing this great info! Sure helped me a lot in getting my Custom Field in SharePoint 2010 to be added to my custom list definition through a feature event receiver. Too bad this can't be done in the list definition directly. This works like a charm as well though.Anonymous
January 18, 2012
Thanks for sharing the way how to do this. Saved me some time. Cheers! And I don't know if Koen is ever going to read this, but it is possible to add custom field directly to custom list definition. It works the way any other field is referenced.Anonymous
August 15, 2013
I have the same scenario. I have defined a solution for creation of Custom Field type and is functioning as expected. And now with in the same solution i need to create a custom list on FeatureActivated event receiver. I was using the above snippet but it didnt work to me. If i give a standard field type like Text to the parameter strFieldType it works. But not to custom field type. I have cross verified the typeName with the ftdType*.xml it matches. And you did have metioned about the below parameter 'Name' attribute to match with the one in FldTypes*, but i donot see any attribute with title as 'Name'. element.SetAttribute("Name", strFieldName); // Setting the Field name registered in the “Fldtypes*.xml” file Pls let me know if i am missing any here.Anonymous
July 17, 2014
hi, if i wanna create a spfielduser type column programmatically in my doc lib, how to achieve this. help is appreciated!