How to: Add a Content Type to a SharePoint List

You can reference content types in the XML for a SharePoint list definition so that each time a user creates a list of that type, Microsoft SharePoint Foundation 2010 includes the content type on the list by default.

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

You can also add content types to an existing list by writing code that uses the SharePoint Foundation object model.

Specifying Content Types in a SharePoint List Definition

Before you can add a content type to a list definition, you must be sure that the list is configured to support content types. The first thing to check is the template from which the list instance is created. If the ListTemplate element has an attribute named DisallowContentTypes, and the attribute is set to TRUE, the list does not support content types. If the attribute is set to FALSE or is missing, the list does support content types.

The next thing to check is whether content types are enabled on the list instance. The Schema.xml file that defines the list instance has a List element. This element must include an attribute named EnableContentTypes, and the attribute must be set to TRUE. Setting the attribute to TRUE is equivalent to selecting Yes under Allow management of content types? in the Advanced Settings section of List Settings.

To add a content type to a list definition, you add a ContentTypes element to the list schema. The ContentTypes element contains a collection of ContentTypeRef elements. Each ContentTypeRef element specifies a site content type that SharePoint Foundation should copy locally to the list, as a list content type, whenever a user creates a new list of the specified type. The ContentTypeRef element contains a single attribute, ID, which you set to the content type ID.

The site content type that you reference must be in scope for the list—that is, it must be declared at the same site level or higher in the site hierarchy. For more information about content type scope, see Content Type Scope.

Note

When SharePoint Foundation creates a list instance, it includes only those columns that are declared in the base type schema of the list or in the list schema. If you reference a site content type in the list schema, and that content type references site columns that are not included in the base type schema of the list or in the list schema, those columns are not included. You must declare those columns in the list schema for SharePoint Foundation to include them on the list.

For more information about site columns, see Introduction to Columns.

To add a content type to a SharePoint list definition

  1. In the list definition XML, add a ContentTypeRef element to the ContentTypes element.

  2. Set the ID attribute of the ContentTypeRef element to the content type ID of the content type that you want to include on the list.

The following example shows partial markup for a list definition schema that includes a ContentTypeRef element.

<List xmlns:ows="Microsoft SharePoint" Title="The List" Direction="$Resources:Direction;" FolderCreation="FALSE" Url="Lists/TheList" BaseType="0" xmlns="https://schemas.microsoft.com/sharepoint/">
  <MetaData>
    <ContentTypes>
      <ContentTypeRef ID="0x01060062efcfca3f4d4036a0c54ed20108fa2e" />
    </ContentTypes>
    ...
  </MetaData>
</List>

For more information about the ContentTypes element in the list definition schema, see ContentTypes Element (List).

Adding Content Types to an Existing SharePoint List

You can use the SharePoint Foundation object model to add content types to an existing list.

To add a content type to a SharePoint list

  1. Use the Lists property to get a collection of lists for the site on which the list is located.

  2. Declare a variable of type SPList, and set it equal to the object in the site list collection that represents the list.

  3. Enable content types for the list by setting the value of the list’s ContentTypesEnabled property to true.

  4. Use the AvailableContentTypes property to access the content types that are available for the site on which the list is located. This method returns an SPContentTypeCollection object.

  5. Declare a variable of type SPContentType, and set it equal to the SPContentType object in the collection that represents the site content type you want to add to the list.

  6. Verify that the list can accept the content type that you have selected by calling the IsContentTypeAllowed(SPContentType) method.

  7. Use the ContentTypes property to access the collection of list content types on the specified list. This method returns an SPContentTypeCollection object.

  8. Use the Add method to add the SPContentType object to the list content type collection.

When you add a site content type to a list using the object model, SharePoint Foundation automatically adds any columns that the content type contains that are not already on the list. This contrasts with referencing content types in a list schema, in which case you must explicitly add the columns to the list schema for SharePoint Foundation to include them in list instances.

Example of Console Application That Adds a Site Content Type to the Shared Documents List

The following example is a console application that adds a site content type to the Shared Documents list in a site. The content type that is used in this example is the same content type as is created by the example for How to: Add a Content Type to a Site.

Console applications are useful for experimenting in a development environment. In a production environment, the code for this example would more likely be included as part of the FeatureActivated method of an SPFeatureReceiver object.

using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("https://localhost"))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {

                    // Get a content type.
                    SPContentType ct = site.AvailableContentTypes["Financial Document"];

                    // The content type was found.
                    if (ct != null) 
                    {

                        // Get a list.
                        try 
                        {
                            SPList list = site.Lists["Shared Documents"]; // Throws exception if does not exist.

                            // Make sure the list accepts content types.
                            list.ContentTypesEnabled = true;

                            // Add the content type to the list.
                            if (!list.IsContentTypeAllowed(ct))
                                Console.WriteLine("The {0} content type is not allowed on the {1} list",
                                                   ct.Name, list.Title);
                            else if (list.ContentTypes[ct.Name] != null)
                                Console.WriteLine("The content type name {0} is already in use on the {1} list",
                                                   ct.Name, list.Title);
                            else
                                list.ContentTypes.Add(ct);
                        }
                        catch (ArgumentException ex) // No list is found.
                        {
                            Console.WriteLine("The list does not exist.");
                        }
                    }
                    else // No content type is found.
                    {
                        Console.WriteLine("The content type is not available in this site.");
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module Test
   Sub Main()
      Using siteCollection As SPSite = New SPSite("https://localhost")
         Using site As SPWeb = siteCollection.OpenWeb()

            ' Get a content type.
            Dim ct As SPContentType = site.AvailableContentTypes("Financial Document")

            If ct IsNot Nothing Then

                ' The content type was found.
                Try

                    ' Get a list.
                    Dim list As SPList = site.Lists("Shared Documents")

                    ' Make sure the list accepts content types.
                    list.ContentTypesEnabled = True

                    ' Add the content type to the list.
                    If Not list.IsContentTypeAllowed(ct) Then
                        Console.WriteLine("The {0} content type is not allowed on the {1} list", ct.Name, list.Title)
                    ElseIf list.ContentTypes(ct.Name) IsNot Nothing Then
                        Console.WriteLine("The content type name {0} is already in use on the {1} list", ct.Name, list.Title)
                    Else
                        list.ContentTypes.Add(ct)
                    End If
                Catch ex As ArgumentException

                    ' No list is found.
                    Console.WriteLine("The list does not exist.")
                End Try
            Else

                ' No content type is found.
                Console.WriteLine("The content type is not available in this site.")
            End If
         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub
End Module

See Also

Concepts

Site and List Content Types

Content Type Scope

Content Type Definitions