SharePoint Developer Productivity Tip #2 – Easy Site Column Features

Almost every SharePoint project involves custom site columns. You can create a site column via the browser, but unless the column is trivial and only used in a single site, you should always deploy site columns using features. The reason for this is that when you create a site column via the browser, SharePoint always creates a new globally unique identifier. If you create a site column with the browser on one site, and then repeat the work on another site, you will have two unique site columns because even though the site columns on both sites are otherwise identical, they have different ID’s.

The different IDs are a problem if you ever need to aggregate the data across the site collections, if you want to use the column in search scopes, and if you are using records center. Even if you have none of these needs, you certainly don’t want to have to create the same site column by hand over and over again on every site where it is needed. And, if your solution requires any custom site columns, it probably requires many of them.

The features schema for site columns, or Fields, isn’t trivial. You can see it here: http://msdn.microsoft.com/en-us/library/ms437580.aspx . I could create my <Field> definitions by hand, but I am much too lazy for that! The browser makes it so easy! Fields in the SharePoint object model are handled by the SPField class. The SPField class has a property, SchemaXml, which contains the XML definition for the field. This XML is exactly the same as the XML required in a feature’s element manifest to define the field.

The fastest and easiest way to make a site column feature is to:

  1. Create the site column on a development site in the browser.
  2. Use a tool to copy the resulting SPField instance’s XmlSchema property value.
  3. Paste it into your element manifest.

It doesn’t get much easier than copy and paste. There are a number of tools out there that you can use to do this, but the one I use is SharePoint Manager 2007 by Carsten Keutmann. Carsten is also the creator of WSPBuilder and I am a big fan of his work. I use the free SharePoint Manager 2007 for many different tasks, some of which I will write about in subsequent SharePoint Developer Productivity Tips!

To get the SchemaXml, simply navigate to the field in SharePoint Manager 2007, select the Schema Xml tab, and copy the text.

Here is the copied schema from the core About Me site column:

<Field
ID="{e241f186-9b94-415c-9f66-255ce7f86235}"

Name="Notes"

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

StaticName="Notes"

Group="_Hidden"

DisplayName="About Me"

ReadOnly="FALSE"

Type="Note"

Sortable="FALSE"

RichText="TRUE"

RichTextMode="FullHtml"

FromBaseType="TRUE" />

Author: Doug Ware