How to create Custom Properties for Custom Field Types in a Feature Definition

One of my colleagues approached me with an issue on which he was banging his head for quire long time. Here is what he was experiencing. He was able to define a custom property for a custom field type he created but he was not able to use that property within a feature created for deploying the site columns.

<Field ID=”{7810D64B-6B69-43da-A007-638BF0F5F4AB}” Type=”CustomFieldType” DisplayName=”Custom Column 1″ Name=”CustomCOl1″ StaticName=”CustomCOl1″ Group=”Custom Columns” MyCustomProperty=”My Property” ></Field>

While installing feature, the element files are always validated against wss.xsd. During that time any custom property specified in the field CAML throws schema validation error.

I was searching a way to bypass wss.xsd. The solution I found was to read the field CAML statements from the elements (in fact I put them in a separate text file within the feature) use them as a parameter to SPFieldCollection.AddFieldAsXml(…) method. This did solve my issue of deploying the site columns as a feature. But still I couldn’t get the property value I specified within the field control class using SPField.GetCustomProperty(…) method. I think I made a mistake somewhere in the field class, but still couldn’t figure out what is going wrong.

Since I was running short on schedule, I decided to try an alternate approach. After bit of googling I found that within a SPField object, all custom properties are stored in a dictionary and has corresponding XML representation using an element <Customization>.

I tried to create a similar XML manually and passed it to the SPFieldCollection.AddFieldAsXml(…) method. The field got installed without any error and I was able to get the property value with SPField.GetCustomProperty(…) method.

<Field Type=”CustomFieldType” DisplayName=”Custom Column 1″ Name=”CustomCOl1″ StaticName=”CustomCOl1″ Group=”Custom Columns” MyCustomProperty=”My Property“><Customization><ArrayOfProperty><Property><Name>MyCustomProperty</Name><Value xmlns:q1=”http://www.w3.org/2001/XMLSchema” p4:type=”q1:string” xmlns:p4=”http://www.w3.org/2001/XMLSchema-instance”>Val1</Value></Property></ArrayOfProperty></Customization></Field>
 

I am still working on few more issues I am facing with custom field types. I hope I can post few more useful tips soon.

0 comments:

Post a Comment

Popular Posts