Custom Field Elements and the SourceID Attribute

Home » SharePoint Development » Custom Field Elements and the SourceID Attribute

Custom Field Elements and the SourceID Attribute

Posted on

Programmatically creating site columns and content types in SharePoint 2010 is a relatively straightforward process, especially with the new project item framework in Visual Studio 2010 and enhancements like CKS:DEV. Create an element file to define the columns, create another to define the content type and specify which columns to include, deploy it and whistle Dixie ’til the cows come home (or at least until you have to update it, which is an entirely different herd of cows). For a basic text field the declarative markup is pretty simple:

<Field ID="{BF2D02C3-8EF3-44A7-8950-13DE6541AF01}" Name="M_ProjectName" DisplayName="Project Name" Description="Project Name is case sensitive" Group="Custom Site Columns" Type="Text" DisplaceOnUpgrade="TRUE" Required="FALSE" MaxLength="255" />

And including it in the content type is also an easy bank shot (just don’t forget to include the brackets in the ID value):

<FieldRef ID="{BF2D02C3-8EF3-44A7-8950-13DE6541AF01}" Name="M_ProjectName" Description="" DisplayName="Project Name" Required="FALSE" howInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE" />

But wait. There’s something missing here that’s not completely obvious at first. While (or whilst, depending upon which side of the pond you’re from) the site column definition contains a value for the Description attribute, if you deploy the code as is the description text will never show up when the field is rendered on the form. This is due, in whole or in part, to the fact that the Field element is missing the SourceID attribute. The documentation states that this is an optional field and I suppose it is – the field will work without it. But omit it and the description text will not be displayed. Changing the markup to the following will fix the issue:

<Field ID="{BF2D02C3-8EF3-44A7-8950-13DE6541AF01}" Name="M_ProjectName" DisplayName="Project Name" Description="Project Name is case sensitive" Group="Custom Site Columns" Type="Text" DisplaceOnUpgrade="TRUE" Required="FALSE" MaxLength="255" SourceID=" http://schemas.microsoft.com/sharepoint/v3" />

Bingo! The description text shows up right where it’s supposed to. This might also affect other functionality that I haven’t stumbled across yet. As a general rule, I always include the SourceID attribute when creating site columns declaratively. I have not tried the same thing using a programmatic approach – it may be inferred under that model without the need to specifically declare it. If anyone has tested it that way I’d like to hear about it.

Happy SharePointing!