Note: This is a repost, the original one was published on SharePoint Blogs on August 12, 2008
When MOSS 2007 was still in beta and features and custom fields were new areas to discover we created the classic regular expression field type too, just to play with and learn the new technology.
Our implementation SPFieldRegEx was inherited from the Text type and had three custom properties defined in the field type definition XML:
The RegEx property stores the regular expression pattern, the MaxLen controls the maximal length of the field content and finally, the ErrMsg holds the validation message to be displayed when the input text does not match with the regular expression.
There is nothing interesting in that up to this point, but if you would like to deploy this custom field using a feature setting custom values to the properties you might encounter some difficulty.
Since I haven’t found that documented neither in the WSS SDK nor on developer blogs in the past years, I decided to share my experience.
If you create your feature definition for the field as you do normally with the built in field types, the result is the following XML:
But if you try to install the feature, you get the following error:
Feature definition with Id 6fd6ca04-3ac3-490f-b22f-4461a2253001 failed validation, file ‘feature_definition2.xml’, line 5, character 299:
The ‘RegEx’ attribute is not allowed.
If you remove the RegEx attribute, the same error message appears with MaxLen, if you remove that too, the ErrMsg causes problem.
So what to do to make this attributes allowed?
The schema of the features is defined in the wss.xsd. Now the most important part for us is the FieldDefinition complexType that is responsible – what a surprise! – for describing the format of the field definitions in the features. Besides other things it contains the list of the allowed attributes.
The fragment above contains only the most widely used attributes for example.
One quick and dirty solution would be to include our custom attributes in the XSD schema but this probably wouldn’t be a supported method. Fortunately in this case MS has left the back door open: if you check the last attribute in the schema, it is anyAttribute with namespace ##other, meaning that you can inject your own attributes in the XML files using your own namespace.
After a minor modification in the feature definition XML (highlighted below) the XML was passed the schema check and our custom field feature was installed successfully.
Update: Although the technique described in this post help to avoid the validation error, the custom properties are not populated. We will discuss the resolution for this issue in the next part.