Second Life of a Hungarian SharePoint Geek

August 12, 2008

How to deploy a custom field with custom properties from a feature

Filed under: Custom fields, Deployment, Features, SharePoint — Tags: , , , — Peter Holpar @ 00:47

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:

 

<PropertySchema>

<Fields>

<Field Name="RegEx" 

DisplayName="Regular Expression" MaxLength="255" DisplaySize="15" 

Type="Text">

</Field>

<Field Name="MaxLen" 

DisplayName="Maximum length" MaxLength="3" DisplaySize="3" 

Type="Integer">

<Default>255</Default>

</Field>

<Field 

Name="ErrMsg" DisplayName="Validation message" MaxLength="255" DisplaySize="30" 

Type="Text">

<Default>The value does not match the regular 

expression</Default>

</Field>

</Fields>

</PropertySchema>

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:

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

<Elements 

xmlns="http://schemas.microsoft.com/sharepoint/">

<Field 

ID="{54634385-A8AC-4898-BF24-E533EB23444F}" Name="RegExField" 

DisplayName="RegExField" StaticName="RegExField" Group="Grepton Fields" 

Type="SPFieldRegEx" Sealed="FALSE" AllowDeletion="TRUE" 

SourceID="http://schemas.microsoft.com/sharepoint/v3/fields" Description="This 

is the RegEx field" RegEx="[0-9]" MaxLen="20" 

ErrMsg="Error!"/>

</Elements>

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.

<xs:complexType name="FieldDefinition" 

mixed="true">

...

<xs:attribute name="Decimals" type="xs:int" 

/>

<xs:attribute name="Description" type="xs:string" 

/>

...

<xs:attribute name="DisplayName" type="xs:string" 

/>

...

<xs:attribute name="FillInChoice" type="TRUEFALSE" 

/>

...

<xs:attribute name="Hidden" type="TRUEFALSE" 

/>

...

<xs:attribute name="Max" type="xs:float" 

/>

<xs:attribute name="Min" type="xs:string" 

/>

...

<xs:attribute name="Name" type="xs:string" 

use="required"/>

...

<xs:attribute name="ReadOnly" type="TRUEFALSE" 

/>

...

<xs:attribute name="Required" type="TRUEFALSE" 

/>

...

<xs:attribute name="Title" type="xs:string" 

/>

<xs:attribute name="Type" type="xs:string" use="required" 

/>

...

<xs:attribute name="ID" type="UniqueIdentifier" 

/>

<xs:attribute name="Group" type="xs:string" 

/>

<xs:attribute name="MaxLength" type="xs:int" 

/>

<xs:attribute name="SourceID" type="xs:string" 

/>

<xs:attribute name="StaticName" type="xs:string" 

/>

...

<xs:anyAttribute namespace="##other" processContents="lax" 

/>

</xs:complexType>

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.

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

<Elements 

xmlns="http://schemas.microsoft.com/sharepoint/" 

xmlns:grp="http://schemas.grepton.com/sharepoint/">

<Field 

ID="{54634385-A8AC-4898-BF24-E533EB23444F}" Name="RegExField" 

DisplayName="RegExField" StaticName="RegExField" Group="Grepton Fields" 

Type="SPFieldRegEx" Sealed="FALSE" AllowDeletion="TRUE" 

SourceID="http://schemas.microsoft.com/sharepoint/v3/fields" Description="This 

is the RegEx field" grp:RegEx="[0-9]" grp:MaxLen="20" 

grp:ErrMsg="Error!"/>

</Elements>

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.

About these ads

3 Comments »

  1. Hi Peter,

    I just ran into this issue – good description of the issue.

    I can’t find the next part (populating the custom properties). Any chance you could link to that.

    My problem with my custom field has a bit of a twist, though. My site template is a 2010 one being generated with SharePoint. So, I’m not writing my .wsp file, and I’m not clear how I might inject my own namespace. Any ideas?

    As it stands, the WSP file is generated, but the solution not activated in the solution store. Annoying.

    Essentially, I want my users to be able to set up sites using my custom field, and then save them as templates in SharePoint 2010

    Comment by Andy Burns — January 26, 2011 @ 11:08

    • Hi Andy,

      It sounds very interesting idea for a custom field!

      To tell the truth I’ve never tested this possibility for SP 2010, and the planned next part of the post was never published because I had not much success in the implementation. I still feel it should be possible some way, but had not enough time to dig into the details.

      Peter

      Comment by Peter Holpar — January 30, 2011 @ 21:06

  2. Hmm … well I found a work-around. Essentially, I couldn’t find a way of getting SP2010 site templates to work with my field. There doesn’t seem to be any way into the WSP generation process to add a new namespace.

    So, in the best spirit of SharePoint hacking, I decided to use the ‘RelatedField’ property instead. This is, actually, an accurate name for the property, but I guess I’m not using it as it was originally intended. And as it is part of the schema it works!

    But I’d love to ask someone from MS exactly how you’re supposed to use Custom Fields in user generated site templates!

    Comment by Andy Burns — January 31, 2011 @ 01:20


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.

Join 53 other followers

%d bloggers like this: