SharePoint 4 Developers

Additional reference guide in .NET / SharePoint Development

Lesson 2 – Content Types – Part I

What are Content Types? This concept is present in WSS 3.0 and remains present in SharePoint 2010 Foundation.

Hi folks,

The SharePoint Lessons Series is back with a very interesting issue for analysts and developers: Content Types.

What are Content Types? This concept is present in WSS 3.0 and remains present in SharePoint 2010 Foundation.

In this post I will present how to create content types, from analysis to development!

Enjoy one more post of the SharePoint Lessons Series!

What are Content Types?

Content Types are collections of metadata (encapsulate a data schema) that can be applied to a particular content such as lists and libraries, enabling you to manage its settings and behaviours in a centralized way.

A metadata is a data that defines other data, it works as an identifier. For example, the version number of an item is an example of metadata because it identifies the data.


The identification of metadata depends on the classification and categorization of the object in question. This classification is called Taxonomy.

Just for understanding of how the classification and categorization of data works, consider 2 types of documents to be used here for example: job application and work contract.

Each metadata must be extracted and documented for the creation of content types. In this case I show how to do it with Job Application and Job Contract, as Figure 1.

Figure 1 - Sample Document Inventory Spreadsheet

The spreadsheet above shows the metadata extracted from the fictitious documents JobAplication.docx and JobContract.docx.

Once classified and categorized, the two content types Job Application and Job Contract may be applied to lists and libraries.

In a technical point of view both content types can be applied to the same library, because multiple content types can be stored in the same list or library, but in a business model perspective this may not necessarily be an ideal thing.

Creating Content Types

The Content Types can be created in 3 ways:

  • By the User Interface
  • Using the API (object model)
  • By installing content types features based on XML definition files.

Now check out the creation of the Content Type of Job Contract in 3 different ways.

How to Create a Content Type by the User Interface

Click Site Actions> Site Settings and click on Site Content Types (group Galleries).

Figure 2 – Site Content Types

Click Create to add a new Content Type.

Figure 3 - Create a Content Type

Please fill out the new content type according to Figure 4 and click OK.

Figure 4 - New Content Type

In the next part of my post I will show how the hierarchy of the types of Content Types.

After creating the Content Type Job Contract, it is necessary to add site columns that will be part of the collection of metadata.

Figure 5 - Content Type Job Contract

Add new columns according table 1 settings. Click Add from new site column.

Figure 6 – Adding new site column



Employee Name

Column Name: Employee Name
Field Type: Single line of text
Description: Employee Name

Contract Duration

Column Name: Contract Duration
Field Type: Single line of text
Description: Duration of the Contract


Column Name: Earnings
Field Type: Currency ($, ¥, €)
Minimum Value (Min): 0
Number of decimal places: 2
Description: Earnings

Contract Date

Column Name: Contract Date
Field Type: Date and Time
Description: Date of the Contract

Table 1 – Site Columns definition

Leave the default settings for the settings omitted. In case of doubt of how to create Site Columns, check out my other post about it.

After creating the site columns, the content type is available and configured as shown in Figure 7.

Figure 7 - Content Type Settings

How to Create a Content Type using the API (object model)

Firstly I will show the final solution and describe the code contained in the solution. I believe this is the easiest way of understanding.

The code created by me is available for download and modification for any reader of my blog.

Note the 2 files in the solution and Program.cs SiteSettings.cs:

Figure 8 - Solution with project Scripts

The SiteSettings class is responsible for creating the SiteColumns described in Table 1 and the Content Type Job Contract.

Below is the code of SiteSettings class:

Code Snippet
  1. //------------------------------------------------------------------------
  2. // Author: Marcel Medina
  3. // Blog:
  4. //
  5. // Feel free to use this code any way you want
  6. // If this code helped you, leave a comment on the blog!
  7. //-------------------------------------------------------------------------
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using Microsoft.SharePoint;
  14. namespace CommonScripts.Scripts
  15. {
  16.     public class SiteSettings
  17.     {
  18.         public static void CreateSiteColumn(SPSite oSite)
  19.         {
  20.             string group = "Custom Columns";
  21.             string fieldName = null;
  23.             //Employee Name
  24.             fieldName = "Employee Name";
  25.             oSite.RootWeb.Fields.Add(fieldName, SPFieldType.Text, false);
  26.             (oSite.RootWeb.Fields[fieldName] as SPFieldText).Description = "Employee Name";
  27.             (oSite.RootWeb.Fields[fieldName] as SPFieldText).Group = group;
  28.             oSite.RootWeb.Fields[fieldName].Update();
  30.             //Contract Duration
  31.             fieldName = "Contract Duration";
  32.             oSite.RootWeb.Fields.Add(fieldName, SPFieldType.Text, false);
  33.             (oSite.RootWeb.Fields[fieldName] as SPFieldText).Description = "Duration of the Contract";
  34.             (oSite.RootWeb.Fields[fieldName] as SPFieldText).Group = group;
  35.             oSite.RootWeb.Fields[fieldName].Update();
  37.             //Earnings
  38.             fieldName = "Earnings";
  39.             oSite.RootWeb.Fields.Add(fieldName, SPFieldType.Currency, false);
  40.             (oSite.RootWeb.Fields[fieldName] as SPFieldCurrency).Description = "Earnings";
  41.             (oSite.RootWeb.Fields[fieldName] as SPFieldCurrency).MinimumValue = 0;
  42.             (oSite.RootWeb.Fields[fieldName] as SPFieldCurrency).DisplayFormat = SPNumberFormatTypes.TwoDecimals;
  43.             (oSite.RootWeb.Fields[fieldName] as SPFieldCurrency).Group = group;
  44.             oSite.RootWeb.Fields[fieldName].Update();
  46.             //Contract Date
  47.             fieldName = "Contract Date";
  48.             oSite.RootWeb.Fields.Add(fieldName, SPFieldType.DateTime, false);
  49.             oSite.RootWeb.Fields[fieldName].Description = "Date of the Contract";
  50.             oSite.RootWeb.Fields[fieldName].Group = group;
  51.             oSite.RootWeb.Fields[fieldName].Update();
  52.         }
  54.         public static void CreateContentType(SPSite oSite)
  55.         {
  56.             SPContentType baseType = oSite.RootWeb.AvailableContentTypes["Document"];
  57.             SPContentType jobContract = new SPContentType(baseType, oSite.RootWeb.ContentTypes, "Job Contract");
  58.             jobContract.Description = "Job Contract";
  59.             jobContract.FieldLinks.Add(new SPFieldLink(oSite.RootWeb.AvailableFields["Employee Name"]));
  60.             jobContract.FieldLinks.Add(new SPFieldLink(oSite.RootWeb.AvailableFields["Contract Duration"]));
  61.             jobContract.FieldLinks.Add(new SPFieldLink(oSite.RootWeb.AvailableFields["Earnings"]));
  62.             jobContract.FieldLinks.Add(new SPFieldLink(oSite.RootWeb.AvailableFields["Contract Date"]));
  63.             oSite.RootWeb.ContentTypes.Add(jobContract);
  64.         }
  65.     }
  66. }


Below is the code that make calls to methods CreateSiteColumn and CreateContentType:

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.SharePoint;
  6. namespace CommonScripts.Scripts
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             using (SPSite oSite = new SPSite("http://sharepoint2007:100"))
  13.             {
  14.                 SiteSettings.CreateSiteColumn(oSite);
  15.                 SiteSettings.CreateContentType(oSite);
  16.             }
  17.         }
  18.     }
  19. }

Download the solution here.

How to Create a Content Type by Feature with XML definition files

In this solution the WSP package was created using WSP Builder tool, which works well together with Visual Studio and allows the developer to focus on the solution and not worry as the WSP package will be created.

This is a productivity tool, it helps to automate a deploy task that is not part of the objective of the solution, which certainly would require a few hours to create a script to generate the WSP file.

Download the tool via the link:

Similarly I will show the final solution and then describe the code contained in the solution.

Note the directory structure and files of the solution:

Figure 9 - Solution with Project of FeatureSettings

The main files to be installed are the XML files, because they contain the creation of Site Columns and Content Type of Job Contract.

Check out the feature.xml file:

Code Snippet
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Feature  Id="d9ad6f23-ee06-4521-a51c-65d35827e774"
  3.           Title="FeatureSettings"
  4.           Description="Job Contract Content Type and Fields"
  5.           Version=""
  6.           ActivateOnDefault="True"
  7.           Hidden="FALSE"
  8.           Scope="Site"
  9.           DefaultResourceFile="core"
  10.           ReceiverAssembly="FeatureSettings, Version=, Culture=neutral, PublicKeyToken=4e843e7c1420247a"
  11.           ReceiverClass="FeatureSettings.FeatureSettings"
  12.           xmlns="">
  13.   <ElementManifests>
  14.     <ElementManifest Location="contract.xml"/>
  15.   </ElementManifests>
  16. </Feature>


The file Contrat.xml is referenced in the file feature.xml and contains all the definitions of Site Columns and Content Types.

Code Snippet
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="">
  3.   <ContentType ID="0x010100852E4863F66E174DBD7CD1A930818A02"
  4.                Name="Job Contract"
  5.                Group="Custom Content Types"
  6.                Description="Job Contract"
  7.                Version="0"
  8.                xmlns=""
  9.                Hidden="False"
  10.                ReadOnly="False"
  11.                Sealed="False">
  12.     <FieldRefs>
  13.       <FieldRef ID="{83782324-6342-4fd3-a9ac-7e8aa0210ebe}" Name="Employee_x0020_Name" />
  14.       <FieldRef ID="{1f0d885a-22e7-4e1f-bc1d-5cd794d9dbcc}" Name="Contract_x0020_Duration" />
  15.       <FieldRef ID="{5205d996-6502-4823-9350-180c4e2f771c}" Name="Earnings" />
  16.       <FieldRef ID="{5b6173f8-d2c8-4cc2-84ac-8fc5541e8464}" Name="Contract_x0020_Date" />
  17.     </FieldRefs>
  18.   </ContentType>
  19.   <Field DisplayName="Employee Name"
  20.          Type="Text"
  21.          Required="FALSE"
  22.          ID="{83782324-6342-4fd3-a9ac-7e8aa0210ebe}"
  23.          StaticName="Employee_x0020_Name"
  24.          Name="Employee Name"
  25.          Description="Employee Name"
  26.          Group="Custom Columns"
  27.          xmlns="" />
  28.   <Field DisplayName="Contract Duration"
  29.          Type="Text"
  30.          Required="FALSE"
  31.          ID="{1f0d885a-22e7-4e1f-bc1d-5cd794d9dbcc}"
  32.          StaticName="Contract_x0020_Duration"
  33.          Name="Contract Duration"
  34.          Description="Duration of the Contract"
  35.          Group="Custom Columns"
  36.          xmlns="" />
  37.   <Field DisplayName="Earnings"
  38.          Type="Currency"
  39.          Required="FALSE"
  40.          ID="{5205d996-6502-4823-9350-180c4e2f771c}"
  41.          StaticName="Earnings"
  42.          Name="Earnings"
  43.          Description="Earnings"
  44.          Min="0" Decimals="2"
  45.          Group="Custom Columns"
  46.          xmlns="" />
  47.   <Field DisplayName="Contract Date"
  48.          Type="DateTime"
  49.          Required="FALSE"
  50.          ID="{5b6173f8-d2c8-4cc2-84ac-8fc5541e8464}"
  51.          StaticName="Contract_x0020_Date"
  52.          Name="Contract Date"
  53.          Description="Date of the Contract"
  54.          Group="Custom Columns"
  55.          xmlns="" />
  56. </Elements>

Note that the Content Type Job Contract references all Site Columns by the ID. The directory structure created is the same found at the 12 Hive (the directory C: \ Program Files \ Common Files \ Microsoft Shared \ web server extensions \ 12)

At this point we are not using the class FeatureSettings, which was created automatically by WSP Builder. In another lesson I will explain how Event Receivers work, but not this time, so comment out the class code.

The files *. bat in the project are batch files to install or uninstall the solution in SharePoint and were written manually.

Other key files (*. snk) and text were created automatically by WSP Builder to sign the assembly and for reference in creating the WSP package respectively.

To deploy the solution, create the WSP package as shown in Figure 10:

Figure 10 - Creating the WSP package

With the WSP package created, run the Install.bat and watch it run by the prompt shown in Figure 11:

Figure 11 – Running the batch file Install.bat

Download the solution here.

Content Types can be created in 3 ways, as exemplified in this post. They all have pros and cons.

We also saw that before creating content types an analysis of what metadata should be considered is necessary. Taxonomy is the answer.

Lesson 2 continues on Content Types in the next post, which shows the hierarchy of the types of Content Types.

Book: SharePoint 2007 Development – WROX


Marcel Medina