Generate your own SPBuiltInFieldId class

SharePoint ships with the SPBuiltInFieldId class which allows you to access the ID’s of all the out-of-the-box available fields using intellisense. This is very important in scenarios when your solution heavy relies on custom development and working with list items. The SPBuiltInFieldId class makes working with Content Types and Columns easier: instead of typing the names you can use the Properties of the SPBuiltInFieldId and get to all the standard SharePoint fields using intellisense.

Using the SPBuiltInFieldId class in custom code

While the SPBuiltInFieldId makes working with standard SharePoint fields easier there is no built in support for your custom created Fields. While you could manually create and maintain a similar class it would be a very tedious process: first of all you would have to copy the contents of your Feature manifest and then make a class of it. Additionally each change made to the Columns/Content Types would have to be reflected in two places: Feature Manifest and the wrapper class. You should know better than that.

T4 once again

In my last article I showed you how to create a T4 template for generating the Feature.xml file. Using a similar concept you can make a T4 template generate a wrapper class for your custom fields:

T4 template for generating custom SPBuiltInFieldId class

All you have to do is to set the name of the group to which all your custom belong and the template will do the rest for you:

Generated custom SPBuiltInFieldId class for custom columns

And then you can use your wrapper class for retrieving field values:

Using custom SPBuiltInFieldId class in custom code

To make the wrapper class even more useful I have included some extra information about each field like Display Name, Internal Name and the Field Id:

Additional information about every Field is included in the Property summary

Even more T4

Depending on how many custom fields your solution includes, having them all in a single class might work counter productive. By modifying the T4 template a little, you can make it generate a separate wrapper class for each Content Type which contains all the fields:

Generated wrapper classes for custom Content Types

Download FieldId T4 Template from CodePlex (2KB, TT)

Download ContentTypes T4 Template from CodePlex (2KB, TT)

Technorati Tags: SharePoint,SharePoint 2007,WSS 3.0,MOSS 2007,Visual Studio


comments powered by Disqus