How to: Add a Content Type to a Site

Last modified: November 01, 2010

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

You can specify content types to include in a custom site definition configuration so that each time a user creates a site of that type, Microsoft SharePoint Foundation makes the content types available on the site by default. After a site is created, you can add content types as part of a Feature.

To specify a content type to include in a custom site definition configuration, you first create the content type as a separate Feature, and then you reference that Feature in some Collaborative Application Markup Language (CAML) markup to the site definition configuration in the Onet.xml file of the site definition. Then, when a user creates a new site of that type, the content type is included by default in the site’s content type collection.

Caution note Caution

Modifying the Onet.xml file for any of the built-in site types of SharePoint Foundation is not supported, so you can use the following procedure only on custom site types. For more information about creating custom site definition configurations, see How to: Create a Custom Site Definition and Configuration.

To specify a content type in a site definition configuration

  1. Create the content type as a separate Feature.

    For more information, see Using Features in SharePoint Foundation.

  2. Reference that Feature in the Configuration element (in the Onet.xml file) that defines your custom site type:

    1. Open the Onet.xml file in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates\site_type\XML, where site_type is name of the custom site definition.

    2. In the Configuration element, add a reference to the content type Feature in either the SiteFeatures or WebFeatures element. You identify the Feature by its GUID. The following is an example.

        <Feature ID="00BFEA71-1C5E-4A24-B310-BA51C3EB7A57" />
        <Feature ID="695B6570-ACDC-4A8E-8545-26EA7FC1D162" />
        <Feature ID="00BFEA71-4EA5-48D4-A4AD-7EA5C011ABE5" />
        <Feature ID="00BFEA71-E717-4E80-DEAF-D0C71B360101" />

      For more information about the difference between these two elements, see SiteFeatures Element (Site) and WebFeatures Element (Site).

You can add content types to a site by using either declarative XML or the SharePoint Foundation object model. Declarative XML uses the Content Type Definition Schema to define content types. Content type definitions are declared in the element manifest file of a Feature, and the content types are added to a site when the Feature is activated.

When you use the SharePoint Foundation object model, you must subclass the SPFeatureReceiver class. Place the code that creates your content types and adds them to the site in the FeatureActivated method.

Typically, declarative XML is easier to write. However, declarative XML generally offers less flexibility than the object model, which has access to the capabilities of the entire Microsoft .NET Framework and can be debugged at run time. Both approaches are supported by templates that are provided with SharePoint development tools in Microsoft Visual Studio 2010.

To add a content type by using declarative XML

  1. Create a Feature.

    For more information, see Using Features in SharePoint Foundation.

  2. Add an element manifest file to the Feature.

  3. Add an Elements element as the root XML element in the element manifest.

  4. Add a ContentType element as the child of the Elements element.

    Note Note

    The value that you use for the ID attribute has a very specific format. For more information, see Content Type IDs.

  5. Add the column references that you want. For more information, see How to: Reference a Column in a Content Type.

  6. Add a DocumentTemplate definition as desired.

    Note Note

    The file that is referenced by the DocumentTemplate element must already exist on the site or must be included with the Feature. For more information, see How to: Provision a File.

  7. Complete the content type definition as desired. For more information, see Content Type Definitions.


The following example is the element manifest for a Feature. When the Feature is activated, the element manifest is used to create a site column and add it to the site column collection on the current site. Then the manifest is used to create a site content type and add it to the content type collection on the current site.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">

  <!-- Create a site column. -->

  <Field ID="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}"
         Group="Financial Columns" />

  <!-- Create a site content type that uses the column. -->

  <!-- Parent ContentType: Document (0x0101) -->
  <ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e"
               Name="Financial Document"
               Group="Financial Content Types"
               Description="Base financial content type"
      <FieldRef ID="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}" Name="Amount" DisplayName="Amount" Required="FALSE"/>


To add a content type by using server code

  1. Create a feature receiver class that inherits from the SPFeatureReceiver class.

  2. In the FeatureActivated method, use the ContentTypes property to access the content type collection for the site.

    This property returns an SPContentTypeCollection object.

  3. Create an SPContentType object. For more information, see Creating Content Types.

  4. Use the Add method to add the SPContentType object to the site content type collection.


The following example shows the implementation of the FeatureActivated method in a subclass of the SPFeatureReceiver class. When the Feature is activated, code in the FeatureActivated method creates a site column and adds it to the site column collection on the current site. Then the code creates a content type that inherits from the Document content type and adds it to the content type collection on the current site.

Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties)
    Dim siteCollection As SPSite = DirectCast(properties.Feature.Parent, SPSite)
    Dim site As SPWeb = siteCollection.OpenWeb()
    ' Create a site column. 
    Dim amountFieldName As String = site.Fields.Add("Amount", SPFieldType.Currency, False)
    Dim amountField As SPFieldCurrency = DirectCast(site.Fields.GetFieldByInternalName(amountFieldName), SPFieldCurrency)
    amountField.Group = "Financial Columns"
    amountField.DisplayFormat = SPNumberFormatTypes.TwoDecimals
    amountField.MinimumValue = 0
    ' Create a site content type. 
    ' Get a content type to be the parent of a new Financial Document content type.
    Dim documentCType As SPContentType = site.AvailableContentTypes(SPBuiltInContentTypeId.Document)
    ' Create the Financial Document content type.
    Dim financialDocumentCType As New SPContentType(documentCType, site.ContentTypes, "Financial Document")
    ' Note: A content type is not initialized until after it is added.
    financialDocumentCType = site.ContentTypes(financialDocumentCType.Id)
    financialDocumentCType.Group = "Financial Content Types"
    ' Add the Amount column. Child content types inherit the column.
    Dim amountFieldRef As New SPFieldLink(amountField)
    ' Commit changes.
End Sub