Site Columns in WSS and MOSS

This article is one in a series of articles about the core WSS/MOSS concepts. This series of articles is focused towards .NET developers who are embarking upon the SharePoint development and who have no prior experience in writing code against SharePoint.

This particular article discusses the concept of a Field, also known as the Site Column; it shows how to create a site column using CAML, what are the important properties of a column and at the end it shows the CAML for creating commonly used columns.

Introduction

As you might already know, most of the data in SharePoint is stored in SharePoint Lists, which in turn store that data in SQL Server. As far as the users of SharePoint are concerned, they store their data in SharePoint Lists and that’s about it.

The data in a list is stored by virtue of storing it in Site Columns (aka Fields). WSS 3.0 introduced the concept of Site Column. Think about a Site Column as the logical storage place for a data element; data element could be any data which you are interested in – it could be the name of a person, company address, amount or any other data that needs to be stored in a list.

When we add an item in a list, we are actually providing values for the site columns contained in that particular list. The following shows the Out-of-the-box (referred to as OOTB from now on) Tasks list which is using the pre-defined site columns.



Fig 1 – SharePoint tasks list which uses the pre-defined columns

WSS and MOSS both come with OOTB columns for representing different types of data. For example, there are columns for storing email, name, address, job title, etc. The following shows some of the OOTB site columns.



Fig 2 – OOTB Site Columns

These site columns are good for 60-70% of the applications which are built on top of SharePoint but there are times when custom columns are needed to store data. Most of the times the custom columns are needed at the time of creating Content Types for the WCM pages (A topic that will be covered later on).

Are you still thinking what the benefits of using Site Columns are? Read on.

Benefits

Since a Site Column is defined once and could be re-used at many places that means it provides re-usability and a way for the Site Designers and the Administrators to enforce rules on what data should be provided for different fields.

For example, the site administrator could define a custom column which displays a list of benefits provided to employees. That custom column could then be reused in all of the SharePoint lists (under all of the child sites under the site collection or site where the Site Column is defined) wherever employees’ benefits information needs to be captured. This approach would make sure that users don’t provide their own versions of data rather they just choose the values from a pre-defined set of values.

Similarly, a custom column could be defined for capturing a person’s driver’s license number; the same column could be set to not exceed a certain number of characters and might also force other formatting rules. That would make sure that the data entered by the user adheres to the business rules set on the site column.

All by itself, a Site Column does not provide much value; a Site Column has to be used in other SharePoint objects/constructs in order to provide some value. Typically a Site Column is used in Content Types and Lists/Libraries.

Create a Site Column

A Site Column could be created by using either of the following methods:
  • SharePoint User Interface
  • CAML – XML syntax for performing many operations in SharePoint.
  • SharePoint Object Model – .NET objects provided by SharePoint.
Before going into any other details about the Site Column, let’s quickly see the syntax for adding a Site Column using CAML. Download the base source project from here All you have to do now is follow the instructions below and compile the project. The details about the Visual Studio source structure are explained here.

Adding a Site Column using CAML

  1. Create a folder named TechConceptionColumns under the FEATURES folder. This could be any name that you like.
  2. Add a file named Feature.xml
  3. Add the following XML fragment to the file and save it.

    <?xml version="1.0" encoding="utf-8"?>
    <Feature
      Id="7A3674BE-D40B-4587-AAD8-E9B4A7F653CE"
      Title="Tech Conception Columns"
      Description="This feature contains the Tech Conception columns."
      Version="1.0.0.0"
      Hidden="false"
      Scope="Site"
      ImageUrl="TechConception\wss.gif"
      xmlns="http://schemas.microsoft.com/sharepoint/">
     
      <ElementManifests>
        <ElementManifest Location="elements.xml" />
      </ElementManifests>
     
    </Feature>
  4. Add another file named Elements.xml
  5. Add the following XML fragment to the file and save it.

    <?xml version="1.0" encoding="utf-8"?>
      <!-- Tech Conception Columns -->
      <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
     
        <!-- Date (Default Formatting - Date/Time) -->
        <Field
          ID="{00A40A0F-EBD1-4daa-9A94-D53CA79D9748}"
          SourceID="http://schemas.microsoft.com/sharepoint"
          Name="TCArticlePublishDateTime"
          StaticName="TCArticlePublishDateTime"
          DisplayName="TC Article Publish Date Time"
          Type="DateTime"
          Group="Tech Conception Columns"/>
     
      </Elements>
     
  6. Open Install.bat and replace http://win2008ent:82 with the URL to the site collection where you want the column to be created.
  7. Build the project. Once the project has been built successfully, the Site Column would have been deployed to the site collection.
  8. Browse the root web site at the site collection that you specified in step 6 and go to the Site Settings -> Site Columns page. Select “Tech Conception Columns” in the Show Group drop-down and it will show you the newly added site column. See below.


Fig 3 – Newly added site column under Site Settings -> Site Columns

Running the complete version of the code adds other columns as well (as shown below).



Fig 4 – All of the custom columns added by the complete project

Properties of a Site Column

If you look at the CAML which is defined in the elements.xml file, you would realize that the XML syntax for defining a field is very simple; we just have to provide values for couple of important attributes of the node and that’s about it.

Let's discuss Site Column in detail and see what are the important attributes related to it.

Id

This is the unique GUID of the field which must be provided in the following format: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} Note that you have to also provide the { }.

Name

A Site Column needs to have a name that is used by SharePoint for internal purposes. This name must be unique among the site columns for a site collection or for a list.

At the time of creating the field (regardless of how the field was created), SharePoint encodes the field name to make sure that spaces and other special characters are properly encoded. The actual internal name for the field is the encoded name created by SharePoint.

Display Name

This is the user friendly name which is displayed in the SharePoint UI. For example, this name is shown in the drop-down list of Site Columns while adding a column to a list, or it is shown in the column header in a list or it is shown as a label when the field is displayed in a form.

Static Name

This is another internal name used by SharePoint; one of the differences between Static Name and Name is that SharePoint does not encode the value provided for the Static Name.

Type

Since we will be using site column for storing data in it, hence it’s logical for a site column to be of a particular “data type”. For example, the salary amount will always be a numeric value; hence that column should of a compatible type.

OOTB SharePoint offers the following column types:



Fig 5 – Out-of-the-box column types

Group

There could be many site columns in a SharePoint site. Therefore it helps to group those columns. OOTB site columns in SharePoint are divided into the following groups:
  • Base Columns
  • Core Contact and Calendar Columns
  • Core Document Columns
  • Core Task and Issue Columns
  • Extended Columns
Typically you would define a group for the system you are working on and all of your custom columns related to that system would be associated to it. In our example, we have defined a custom group called Tech Conception Columns which is automatically created by SharePoint when we install the above shown CAML.

Description

This attributes contains a descriptive text for specifying the intended purpose of the site column.

Required

This attribute specifies whether or not the site column is required. This is used by SharePoint for enforcing the business rule.

If a site column is required and the user does not provide a value for that column while adding/updating a list item, SharePoint will display an error message.

Min

This attribute specifies the minimum numeric value that a site column should contain. This is used by SharePoint for enforcing the business rule.

For example, you could define a Salary column for storing the salary of a person; that column could be set to disallow the salary below the minimum salary for the State.

Max

Similar to Min except that it restricts the user from entering a numeric value which is greater than the one defined by this attribute.

Max Length

This attribute specifies the maximum number of characters that can be entered in that site column. Again, this can be used for enforcing the business rules.

CAML for creating commonly used site columns and the result of using that CAML

The following table shows the CAML for creating commonly used site columns and shows how the column looks after it has been created.

CAML Resulting Column
  <!-- Boolean -->
  <Field
    ID="{C80D6D14-52ED-4933-B861-D905EA1290CB}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCBoolean"
    StaticName="TCBoolean"
    DisplayName="TC Boolean"
    Type="Boolean"
    Group="Tech Conception Columns"/>
  <!-- Choice - Drop Down -->
  <Field
    ID="{70D36758-FC8C-4127-8603-574E15F3CE0C}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCChoice"
    StaticName="TCChoice"
    DisplayName="TC Choice"
    Type="Choice"
    Format="Dropdown"
    Group="Tech Conception Columns">
 
    <CHOICES>
      <CHOICE>
        Choice 1
      </CHOICE>
      <CHOICE>
        Choice 2
      </CHOICE>
    </CHOICES>
  </Field>
  <!-- Choice - Drop Down - Fillable -->
  <Field
    ID="{C79C9897-9EFC-4d46-8B37-E45A8025F74F}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCChoiceFillable"
    StaticName="TCChoiceFillable"
    DisplayName="TC Choice Fillable"
    Type="Choice"
    Format="Dropdown"
    FillInChoice="TRUE"
    Group="Tech Conception Columns">
 
    <CHOICES>
      <CHOICE>
        Choice 1
      </CHOICE>
      <CHOICE>
        Choice 2
      </CHOICE>
    </CHOICES>
  </Field>
  <!-- Choice - RadioButtons -->
  <Field
    ID="{986A89D7-81F9-47cc-9F35-55188E13448D}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCRadioButtons"
    StaticName="TCRadioButtons"
    DisplayName="TC RadioButtons"
    Type="Choice"
    Format="RadioButtons"
    Group="Tech Conception Columns">
 
    <CHOICES>
      <CHOICE>
        Choice 1
      </CHOICE>
      <CHOICE>
        Choice 2
      </CHOICE>
    </CHOICES>
  </Field>
  <!-- Choice - RadioButtons - Fillable -->
  <Field
    ID="{E4D359EC-1D72-4e96-8B31-8A683292E1DE}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCRadioButtonsFillable"
    StaticName="TCRadioButtonsFillable"
    DisplayName="TC RadioButtons Fillable"
    Type="Choice"
    Format="RadioButtons"
    FillInChoice="TRUE"
    Group="Tech Conception Columns">
 
    <CHOICES>
      <CHOICE>
        Choice 1
      </CHOICE>
      <CHOICE>
        Choice 2
      </CHOICE>
    </CHOICES>
  </Field>
  <!-- Currency -->
  <Field
    ID="{15FE8F8A-E4DC-4b1d-BE45-A5CCA008C5D9}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCCurrency"
    StaticName="TCCurrency"
    DisplayName="TC Currency"
    Type="Currency"
    Min="10"
    Max="100"
    Group="Tech Conception Columns"/>
  <!-- Date (Date/Time) -->
  <Field
    ID="{00A40A0F-EBD1-4daa-9A94-D53CA79D9748}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCArticlePublishDateTime"
    StaticName="TCArticlePublishDateTime"
    DisplayName="TC Article Publish Date Time"
    Type="DateTime"
    Group="Tech Conception Columns"/>
  <!-- Date (Date Only) -->
  <Field
    ID="{DB20BBAA-7606-4d87-BF79-974FB56B0ECB}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCArticlePublishDate"
    StaticName="TCArticlePublishDate"
    DisplayName="TC Article Publish Date"
    Type="DateTime"
    Format="DateOnly"
    Group="Tech Conception Columns"/>
  <!-- MultiChoice -->
  <Field
    ID="{254500EB-12CB-4bc7-BA83-565297F6ABFA}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCMultiChoice"
    StaticName="TCMultiChoice"
    DisplayName="TC MultiChoice"
    Type="MultiChoice"
    Group="Tech Conception Columns">
 
    <CHOICES>
      <CHOICE>
        Choice 1
      </CHOICE>
      <CHOICE>
        Choice 2
      </CHOICE>
    </CHOICES>
  </Field>
  <!-- MultiChoice - Fillable -->
  <Field
    ID="{38EF168E-BC94-449d-8CBB-8323D1638A39}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCMultiChoiceFillable"
    StaticName="TCMultiChoiceFillable"
    DisplayName="TC MultiChoice Fillable"
    Type="MultiChoice"
    FillInChoice="TRUE"
    Group="Tech Conception Columns">
 
    <CHOICES>
      <CHOICE>
        Choice 1
      </CHOICE>
      <CHOICE>
        Choice 2
      </CHOICE>
    </CHOICES>
  </Field>
  <!-- Note -->
  <Field
    ID="{7AF44986-3B63-4a37-A930-9EB46274025D}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCNote"
    StaticName="TCNote"
    DisplayName="TC Note"
    Type="Note"
    NumLines="10"
    RichText="TRUE"
    Group="Tech Conception Columns"/>
  <!-- Number -->
  <Field
    ID="{F92C6AA5-73AC-4091-BAFE-07F9DBEC2A69}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCNumber"
    StaticName="TCNumber"
    DisplayName="TC Number"
    Type="Number"
    Min="1"
    Max="100"
    Group="Tech Conception Columns"/>
  <!-- Text -->
  <Field
    ID="{7E7E52DD-9E16-4fc3-B57A-0A80F24E0F67}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCText"
    StaticName="TCText"
    DisplayName="TC Text"
    Type="Text"
    MaxLength="15"
    Group="Tech Conception Columns"/>
  <!-- URL -->
  <Field
    ID="{14554132-5038-463e-8074-F67A086B4199}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCURL"
    StaticName="TCURL"
    DisplayName="TC URL"
    Type="URL"
    Group="Tech Conception Columns"/>
  <!-- User -->
  <Field
    ID="{05A2C582-2700-4c16-B76A-FC9E717CB285}"
    SourceID="http://schemas.microsoft.com/sharepoint"
    Name="TCUser"
    StaticName="TCUser"
    DisplayName="TC User"
    Type="User"
    Group="Tech Conception Columns"/>

The complete project can be downloaded from here.

Summary

In this article we covered the basics of creating Site Column using CAML. We also saw examples of creating commonly used Site Columns.

Stay tuned for next articles in this developer series.

blog comments powered by Disqus
 

article resources