Creating SharePoint Site Columns using a Feature


In this post we will be creating some Site Columns and provisioning them into SharePoint using a feature. The tools we will be using are Visual Studio 2008 and WSPBuilder.

First create a new Visual Studio project using the WSPBuilder Empty project template.

sc00

Your new solution should look something like this.

sc01

Add a new project item to the solution using the WSPBuilder Blank Feature template.

sc01a

Name your feature something sensible and open the feature.xml feature definition file.

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="adc873b4-fe40-4169-8304-c9a1dd697c2b"
          Title="MySiteTypes"
          Description="Description for MySiteTypes"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Site"
          ActivateOnDefault="FALSE"
          ImageUrl="GenericFeature.gif"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="sitecolumns.xml"/>
  </ElementManifests>
</Feature>

Update the feature.xml as above. The feature.xml generated by WSPBuilder is generally all you need, the changes I’ve made are to the Description attribute and the Scope attribute. Site Columns must be scoped at the Site Collection level.

I’ve added 2 other attributes;

  1. ActivateOnDefault – Enable/Disable activating this feature when the feature is installed.
  2. ImageURL – an image URL (relative to …/_layouts/images) to display against the feature in the feature gallery.

Now rename the features single element manifest file from elements.xml to sitecolumns.xml, open sitecolumns.xml and add the following XML.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
	<Field Type="Number" DisplayName="MyReference" Required="FALSE" Decimals="0" Commas="FALSE"
			 Group="My Group" ID="{918BA6B8-60BC-417e-AEE5-F9E1A0A59144}"
			 StaticName="MyReference" Name="MyReference">
		<Default>0</Default>
	</Field>
	<Field Type="Choice" DisplayName="MyType" Description="Description of MyType"
			 Required="TRUE" Format="RadioButtons" FillInChoice="FALSE" Group="My Group"
			 ID="{1FDC83BE-D596-45e2-9DD3-AD3338798784}" StaticName="MyType" Name="MyType">
		<Default>Person</Default>
		<CHOICES>
			<CHOICE>Person</CHOICE>
			<CHOICE>Place</CHOICE>
			<CHOICE>Thing</CHOICE>
		</CHOICES>
	</Field>
	<Field Type="Note" DisplayName="MyDetails" Description="Description of MyDetails"
			 Required="FALSE" NumLines="5" RichText="FALSE" Sortable="FALSE" Group="My Group"
			 ID="{AB31BEFA-A178-4a1e-B324-B73E9C15D5A8}" StaticName="MyDetails" Name="MyDetails"
			 AllowHyperlink="TRUE" RichTextMode="Compatible" IsolateStyles="FALSE" AppendOnly="FALSE">
		<Default>Unknown</Default>
	</Field>
</Elements>

I’ve added 3 site solumns of type Number, Choice and Note (multi-line text field). Each field definition must have a unique ID value which is a GUID, and for clarity in this example I’ve grouped these fields together using the Group attribute.

There is quite a lot of CAML there even just for 3 fields, you can craft this manually yourself, but there is an easier way. You create the site columns using the UI, when you’re happy with them you use a third party tool to export the site column definitions concerned then take the output XML and place it in your feature. There’s a couple of tools I use, one is a custom STSADM command by Andrew Connell, the other is the SharePoint Manager which is an explorer like tool which allows you to drill down to field definitions to view the XML, it’s then just a copy and paste into your feature.

When exporting site column definitions this way, you must create new GUID ID values for each field.

More information about the Field definition schema can be found here and here on MSDN.

When naming fields I would usually set the Name and StaticName attributes to values without any whitespace and use the DisplayName attribute to give the field a more informative display name.

Now use the WSPBuilder Build and Deploy commands to build and deploy the Solution to SharePoint.

sc02a

After the solution has been deployed, browse to your SharePoint Site Collection and go to Site Settings – Site Collection Features and activate the MySiteTypes feature.

sc05

Then navigate to Site Settings – Site Columns under the Galleries section, and you should see your new site columns listed under the custom group “My Group”.

sc06

You can now use these site columns when you create Lists or Document Libraries, from the List or Document Library settings page choose Add from existing Site Column.

sc02b

Choose the columns to add to the list (check “Add to default View”), click OK and your custom site columns will be added to the List or Document Library.

sc07

Site Columns on their own however, aren’t entirely useful, it’s more usual to define sets of Site Columns, and using them to form Content Types, which in SharePoint are a whole lot more useful and represent a functional leverage point.

Creating a Content-Type using a feature is relatively straight forward, and we’ll do that in another post by building on the site columns built during this article.

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

3 thoughts on “Creating SharePoint Site Columns using a Feature

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.