How to: Programmatically Create Content Types

Overview

In Microsoft® SharePoint® 2010, you can declare content types in a feature or you can create them programmatically. In SharePoint 2007, it was not possible to programmatically create a content type with a fixed content type ID.

Programmatically created content types are more flexible and provide more granular control over how and when updates to the content type are made during upgrades. In contrast, you cannot change the declarative definition of a content type that was created by using Collaborative Application Markup Language (CAML) after the content type is activated—instead, you must update the content type programmatically.

Note

Note: You can declaratively add columns to a content type in SharePoint 2010 by using the AddContentTypeField feature element.

Summary of Steps

Creating a content type is composed of two basic actions:

  • Step 1: Create the Site Columns. In this step, you create the site columns that the content type will use. Typically, site columns are created declaratively rather than programmatically. However, to provide a complete example, this how-to topic creates a site column programmatically.
  • Step 2: Create the Content Type Programmatically. In this step, you first confirm that the content type does not already exist, and then you create the new content type.

Note

Note: In most cases, you should use a feature receiver class to programmatically create content types when the feature is activated. Content types are typically created on the root Web of a site collection to maximize reuse. In this case, you should use a site-scoped feature.

Step 1: Create the Site Columns

To create the site columns

  • Create the site columns as required. The following example uses the AddFieldAsXml method to programmatically create a site column from an XML field definition. Note that to avoid errors, the code first checks whether the column already exists.

    public static readonly Guid MyFieldId = 
      new Guid("{891B57CF-B826-4B0C-9EDF-8948C824D96F}");
    public static readonly string MyFieldDefXml =
      "<Field ID=\"{CC1E421C-29BE-4373-81D0-55D5D64B2E3D}\"" +
      " Name=\"MyFieldName\" StaticName=\"MyFieldName\"" +
      " Type=\"Text\" DisplayName=\"My Field Name\"" +
      " Group=\"My Columns\" DisplaceOnUpgrade=\"TRUE\" />";
    
    if (web.AvailableFields.Contains(MyFieldId) == false)
    {
      web.Fields.AddFieldAsXml(MyFieldDefXml);
    }
    

    Note

    Note: In this example, the web variable is an SPWeb instance that represents the root Web of a site collection.

Step 2: Create the Content Type Programmatically

To programmatically create a content type

  1. Check whether the content type already exists. If it does not exist, create the content type and add it to the ContentTypes collection for the Web, as shown in the following example.

    public static readonly SPContentTypeId myContentTypeId
      = new SPContentTypeId("0x010100FA0963FA69A646AA916D2E41284FC9D9");
    
    SPContentType myContentType = web.ContentTypes[myContentTypeId];
    
    if (myContentType == null)
    {
      myContentType = new SPContentType(myContentTypeId,
        web.ContentTypes, "My Content Type");
    
      web.ContentTypes.Add(myContentType);
    }
    
  2. Retrieve the site column(s) created in step 1 from the AvailableFields collection of the Web. Add the field to the content type by creating a SPFieldLink instance for the field, and then adding the field link to the content type, as shown in the following example. Repeat this procedure for each column that you want to add to your content type.

    SPField field = web.AvailableFields[MyFieldId];
    SPFieldLink fieldLink = new SPFieldLink(field);
    
    if (myContentType.FieldLinks[fieldLink.Id] == null)
    {
        myContentType.FieldLinks.Add(fieldLink);
    }
    
  3. Call the Update method on the new content type, as shown in the next example. Specify a parameter value of true if you want to push down the changes to content types that inherit from the new content type. For example, specify true if you are adding a column to a site content type that is already used in lists.

    myContentType.Update(true);