共用方式為


How to: Add a Content Type to a List

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

You can reference content types to be included in a list definition, so that each time a user provisions a list of that type, Windows SharePoint Services includes the content type on the list by default.

List definitions can be included in Features. For more information, see Working with Features.

After a user provisions a list, you can add content types to that list through the Windows SharePoint Services object model.

Specifying Content Types in a List Definition

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 Windows SharePoint Services should copy locally to the list, as a list content type, whenever a user provisions 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 you reference must be in scope for the list—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.

The List element includes an attribute, AllowMultipleContentTypes, that must be set to TRUE for multiple content types to be provisioned on the list. In addition, the List element includes an attribute, EnableContentTypes, that must be set to TRUE for Windows SharePoint Services to display multiple content types on the list.

Note

When Windows SharePoint Services provisions a list, it provisions only those columns 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 provisioned on the list. You must also include those columns in the list schema for Windows SharePoint Services to provision them on the list. For more information about site columns, see Introduction to Columns.

To add a content type to a list definition

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

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

Adding Content Types to a Provisioned List

You can add content types to a provisioned list through the Windows SharePoint Services object model.

To add a content type to a list definition

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

  2. Declare an object 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.

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

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

The site content type you add 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.

When you add a site content type to a list using the object model, Windows SharePoint Services automatically adds any columns that content type contains that are not already on the list. This is in contrast to provisioning a list schema with content types, in which case you must explicitly add the columns to the list schema for Windows SharePoint Services to provision them.

See Also

Concepts

Introduction to Content Types

Site and List Content Types

Content Type Scope