共用方式為


Site and List Content Types

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

You create and manage content types at the site level. When you create a new content type and add it to a site's collection of content types, it becomes what is known as a site content type. At this point, the content type is available to add to lists and document libraries but has not yet been added.

When you do add a site content type to a list, Microsoft SharePoint Foundation makes a local copy of the site content type and adds the copy to the list. This local instance is called a list content type and applies only to the list onto which it was copied.

Site Content Types

When you create a content type and add it to a site's content type collection, the new content type becomes available to any child site and also to the site where it was created. For example, if you create a site content type at the root site of a site collection, that site content type becomes available on any site in the site hierarchy. If you add a new content type to a site that is lower down in the hierarchy, it is available in the site where you add it and in any sites below that site in the hierarchy. For more information, see Content Type Scope.

You can review a list of the content types that are available to a site by selecting Site Settings in the user interface. When the Site Settings page appears, look for the Galleries section, and then click Site content types. To get a similar list programmatically, create an object to represent the site by instantiating the SPWeb class in server code or the Web class in client code. Then iterate through the collection that is returned by the AvailableContentTypes property of the object.

The collection that is returned by the AvailableContentTypes property is read-only. This is because the collection includes not only the content types that are defined on the current site but also those that are defined on sites above it in the site hierarchy. To add a new content type or to delete an existing one, you must reference a collection that you can modify. Do this by accessing the ContentTypes property of a site object. You can add content types to the collection that is returned by this property by calling the Add method, and you can delete content types by calling the Delete method.

For information about adding site content types, see How to: Add a Content Type to a Site. For information about deleting site content types, see the Delete method.

Note

To create or manage a content type on a site, you must have Web Designer access rights to that site. Specifically, you must have the Manage Lists and Add and Customize Pages access rights on the site where the site content type resides.

List Content Types

Just as each site has a content type collection, so too does each list or library in a site. You add a content type to a list or library by adding it to the content type collection for the list or library.

You can create a content type collection for a new list as part of the list definition before list instances are provisioned. Within the list definition's ContentTypes element, you can reference existing site content types, and you can also define new content types. In this case, the new content types are available only to list instances that are created from the same list definition. The new content types are not part of the site collection and are not available to other lists. For more information about creating a list content type in a list definition, see How to: Add a Content Type to a SharePoint List.

After a list is provisioned, you can still add content types to the list's content type collection, but any content type that you add must already exist as a site content type that is within scope for the list. You can verify this in the user interface by inspecting the List Settings page for a list that has content types enabled. In the Content Types section of the page is a link that is labeled Add from existing site content types. Clicking the link opens a page where you can choose from available site content types and add them to the list's content type collection.

Important

Before adding a site content type to a list, you should first make sure that the list supports content types. To do this in the user interface, select List Settings, and then select Advanced Settings. Under Allow management of content types, click Yes. To do the same thing programmatically, get a reference to the SPList object in server code or the List object in client code; then set the ContentTypesEnabled property to true.

To add a site content type to a list programmatically, choose a content type from the collection that is returned by the AvailableContentTypes property of an SPWeb object (server) or a Web object (client). Then add the content type to the list’s collection by accessing the ContentTypes property of the list object and calling the Add method of the collection object. For more information, see How to: Add a Content Type to a SharePoint List.

Note

To add a content type to a list, you must have List Administrator rights to that list.

However, it is important to know that you cannot add every content type that is available in a given site to every list or library in the site. Any content type that you add to a document library must inherit from the built-in Document content type or from a content type that is derived from Document. Conversely, any content type that you add to a list must not derive from Document. For more information, see Base Content Type Hierarchy.

Effects from Adding a Site Content Type to a List

When you add a site content type to a list or library, SharePoint Foundation makes local copies of any columns that are referenced by the content type and adds them to the list's fields. For more information, see Fields and Field References.

In addition, SharePoint Foundation makes a local copy of the site content type and adds the copy to the content type collection on the list. The new list content type is a child of the site content type. The value of the Id property for the list content type is different from the value of the Id property for its parent site content type, but otherwise the two content types are initially the same.

The fact that list content types are often derived from site content types has implications for change management. One consequence is that customizations to list content types cannot easily be applied to other lists. For example, a user might edit a list content type by adding a new column. This change would be limited to the content type on the list. The new column would not be added to the site content type from which the list content type derives, nor would any other changes carry over. Changes to a derived content type are never pushed up to the parent content type.

However, a list content type can be affected by changes that are made to its parent. For example, a user could edit a site content type, perhaps by adding a column, and then choose to update all content types that inherit from it. The result could overwrite customizations that users have made to derived site and list content types. It is possible to protect a content type from receiving changes that are made to its parent, but this step has implications for content type management. For more information, see Content Type Change Control.

For these reasons, it is often good practice to apply changes to content types at the site level and limit customizations at the list level.

For information about propagating changes to content types, see Updating Content Types. For an example that shows how to discover where a site content type is being used, see the SPContentTypeUsage.GetUsages method.

See Also

Tasks

How to: Add a Content Type to a Site

How to: Add a Content Type to a SharePoint List

Concepts

Content Type Scope

Creating Content Types

Adding Columns in Content Types to Lists