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)


Possibly related posts

7 Responses to “Generate your own SPBuiltInFieldId class”

  1. Twitter Trackbacks for Generate your own SPBuiltInFieldId class - Waldek Mastykarz [mastykarz.nl] on Topsy.com Says:

    [...] Generate your own SPBuiltInFieldId class – Waldek Mastykarz blog.mastykarz.nl/generate-spbuiltinfieldid-class – view page – cached 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 — From the page [...]

  2. Wouter van Vugt Says:

    Waldek,

    I like the idea of creating your own classes for storing field IDs. Something I've been doing for quite some time.
    Is this solution more manageable than just doing this:
    class FieldID
    {
    public readonly Guid MyFieldID= new Guid("aabbccdd");
    }
    IMHO, a T4 template indicates a changing set of content, but FieldIDs are as static as can be…

    My 2

    Wouter

  3. Waldek Mastykarz Says:

    @Wouter: What I like about it is that it's automatic and always up-to-date. Like you mentioned: the Content Types are quite static once they're done, but so are CRUD methods for a database, right?

  4. Danny Says:

    Hello Waldek,

    When I run template I get IO exception on my SharePoint URL. I am sure it is the correct URL.

    Any idea what could be the problem?

    Danny

  5. Danny Says:

    The problem seems to be that the Template build engine is x86. But I am trying to connect to SP2010. Same problem when you run x86 console app against SP2010.

  6. Waldek Mastykarz Says:

    @Danny: Does building the project throw any errors or is it only the template?

  7. Danny Says:

    Hello Waldek,

    Sorry for late response but I forget all about this thread.
    Problem is that t4 doesn't support x64. So I created a new file and use the client object model to generate class. See file here:

    http://www.dannyhansen.nl/sharepoint/adding-your-own-custom-fields-to-thet-sharepoint-spbuiltinfieldid-class

Leave a Reply

Security Code:

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS
Copyright © 2007 - 2012 Waldek Mastykarz

Creative Commons License