Adding site columns to Office SharePoint Server by using Features

As described in the previous item, one of the ways to deploy a site columns is by using Features. Features are another important new feature J in MOSS 2007. They are an easy way to deploy changes to a SharePoint environment. You can deploy site columns, content types, assemblies, webpart, menu items and a lot more. This article will not talk about features, but is using features to deploy a site column.

 

Please note that the code samples you will see are based on a beta version of SharePoint 2007. The actual syntax can be different in later builds of the product.

 

Features are deployed as files on the server. The can be found in the “FEATURES” folder, which is a subfolder of the “TEMPLATE” folder. In this FEATURES folder, I have created a new subfolder called “fieldstst”. In this folder are a number of files.

 

Feature.xml

The feature.xml file is a general description of the feature and looks like this:

 

<Feature  Id="CA7BD552-10B1-4563-85B9-5ED1D39C073B"
  Title="TSTFields"
  Description="Fields added through features by Ton Stegeman"
  Version="12.0.0.0"
  Scope="Site"
  xmlns=http://schemas.microsoft.com/sharepoint/>
  <ElementManifests>
    <ElementManifest Location="fieldstst.xml"/>
  ElementManifests>
Feature>

 

The most important setting here are the Scope, that sets the scope for the feature. This Scope determines the availability of the feature. I have set it to “Site”, which makes my sitecolumns available at the site level. The Location attribute of the ElementManifest element is a reference to the file that has the actual definition of my site column.

 

Site column feature xml

The file fieldtst.xml looks like this:

 

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field ID="{1DAB9B48-2D1A-47b3-878C-8E84F0D322CB}"
    Name="RegionFromFeature"
    Group="TST_Columns"
    Type="Choice"
    DisplayName="Region"
    SourceID=http://schemas.microsoft.com/sharepoint/v3/fields
    StaticName="RegionFromFeature"
    FillInChoice="FALSE">
    <CHOICES>
      <CHOICE>GlobalCHOICE>
      <CHOICE>EuropeCHOICE>
    CHOICES>
    <Default>GlobalDefault>
  Field>
Elements>

 

I think this XML does not need a very detailed explanation. If you are used to working with site definitions and list templates in SharePoint/WSS 2003, you will recognize the syntax.

 

Installation

The actually use the site column, the feature must be installed and activated. This is done in 2 steps by using STSADM:

 

The first step is to install the feature. In my case this will be like this:

 

stsadm -o installfeature -filename fieldststfeature.xml

 

The second step is to activate the feature:

 

stsadm -o activatefeature -filename fieldststfeature.xml -url http://tonportal

 

After activating, you need to do an IISRESET, and the site column should be available. In my case it is only available in the “tonportal” portal.

 

Leave a Reply