Building Block: SharePoint 2010 Content Types

A SharePoint content type defines the metadata, workflow, and behavior for a category of items in a SharePoint Foundation list or document library.

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

For example, you could define an Expense Report content type so that expense report documents are always created using a particular Microsoft Excel template, and an Approval workflow always starts automatically whenever a new expense report is submitted. If you then add the content type to a document library, your users can create and submit a new expense report by selecting Expense Report from the menu that is attached to the New Document button on the library.

A key benefit that content types provide is that they enable you to manage the underlying schema for a particular category of information in a centralized, reusable way. Content types are defined at the site level, independently of any list or document library in the site. You can use the same content type on many different lists or document libraries throughout a site; in some cases, you can use a content type on lists and libraries throughout an entire site collection. Conversely, you can also use several different content types on the same list or document library. When a list or document library supports multiple content types, each content type appears on the New button, and each can have a unique set of columns, workflows, and behavior.

The availability of a content type is called its scope. The scope of a content type always includes the site where the content type is defined plus every child of that site. For example, imagine a site hierarchy in which a parent site named Europe has child sites named Italy, France, and Germany. New content types added to the parent Europe can be applied to lists or document libraries throughout the sites Europe, Italy, France, and Germany. Because scope extends downward through the site hierarchy, adding a content type to the root site of a site collection makes the content type available to lists and libraries throughout the site collection.

Content types are defined using the principles of inheritance. You never create a content type from scratch. Instead, you select an existing content type as the basis for any new content type that you create. When you base a new content type on an existing content type, the new content type inherits the metadata, workflows, and behavior of the parent content type. This lets you define base content types much as you would define base classes. For example, you could define a content type named Financial Document with characteristics common to all financial documents in your organization. You could then use Financial Document as the basis for two new content types, Invoice and Purchase Order. The new content types will inherit metadata and behavior from Financial Document, so you define common elements only once, in the parent content type.

You can see this pattern at work if you view the Site Content Types gallery under Site Settings. The most basic content type that can be applied to a list is called Item. The content types Announcement, Contact, Task, and many others inherit from Item. Similarly, the most basic content type that can be applied to a document library is Document. The content types Picture, Form, and Wiki Page all inherit from Document.

Note that when you create a new content type, you must decide whether you intend for it to be used on lists or on document libraries. The only content types that can be applied to a document library are content types that inherit from Document. Moreover, the only content types that can be applied to a list are those that inherit from Item but not from Document.

One important departure from the principles of inheritance is that changes to parent content types are not automatically propagated to derived content types. Changes are propagated in a way that is similar to how security permissions are propagated down the file system hierarchy in the NTFS. For more information, see Updating Child Content Types.

Object Model for SharePoint Content Types

SharePoint Foundation has a rich object model that you can use to create, find, add, update, and delete content types. If you are writing code that will execute on the server, use members of the Microsoft.SharePoint namespace. Most types in this namespace have equivalents in the Microsoft.SharePoint.Client namespace that you can use in writing code that will execute on the client.

Finding SharePoint Content Types

You can find out which content types are available for use at any given scope by accessing the AvailableContentTypes property of an SPWeb (server) object or a Web (client) object. Both properties return a read-only collection of SPContentType (server) or ContentType (client) objects. The collection is enumerable and also has several indexers, including the content type name.

You can find out which content types are applied to a list or document library by accessing the ContentTypes property of an SPList object in server code or the same property of a List object in client code. This property returns an enumerable, indexed collection, but in this case the collection is writable.

You should be aware that when a site content type is applied to a list or document library, it is not actually the site content type that is added but a copy of the site content type. Moreover, the list’s copy of the content type has a different content type identifier. It is derived from the identifier for the site content type, but it is not the same identifier. What this means is that you might not be able to retrieve the content type that you want by using a content type identifier as an index into a collection. You should instead use the collection’s BestMatch method. For more information, see Site and List Content Types and Content Type IDs.

If you want to know where a site content type is being used, either on a list or as the parent of another site content type, you can call the static method GetUsages(SPContentType), a member of the SPContentTypeUsage class.

Creating SharePoint Content Types

You can create and deploy a custom content type as part of a Feature. For more information about Features, see Building Block: Features.

Place code to create your content type in the FeatureActivated(SPFeatureReceiverProperties) method of an SPFeatureReceiver object. If your content type uses new site columns, you must create and provision the columns first, before you reference them in the content type. For more information about columns, see Building Block: Columns and Field Types.

To create a content type object, call one of the constructors for the SPContentType class. A parameter of both constructors is an SPContentTypeCollection object. This is the object that is returned in the ContentTypes property of the SPWeb object for the site where the content type will be created. Although you specify a site content type collection in the constructor, the constructor does not add it to the collection. You must do this in a separate call to the same collection’s Add method.

Adding and Deleting SharePoint Content Types

To apply a content type to a list or document library, you must first set the AllowContentTypes property of the list to true and then call the Add method of the collection that is returned in the ContentTypes property of the SPList (server) object or the List (client) object.

To add a new content type to a site, call the Add method of the collection that is returned in the ContentTypes property of an SPWeb (server) object or the Web (client) object.

To remove a content type from a list or document library, you can call the Delete method, passing an SPContentTypeId (server) structure or a ContentTypeId (client) structure that identifies the content type you want to delete. However, you cannot delete a content type that is in use. If you are trying to remove a content type from a list, you must first ensure that no list items use the content type. One way to do that is to iterate through the items in the list and look at the value of each item’s ContentType property. If you are trying to remove a content type from the site collection where it is defined, be sure that the GetUsages method returns an empty list—that is, that the content type is not used on any list and is not the parent of any child content type.

Updating SharePoint Content Types

SharePoint Foundation provides a mechanism for controlling changes to content types through the ReadOnly property and the Sealed property of the SPContentType (server) and ContentType (client) objects that represent content types. You should check the settings of these properties before you try to modify a content type. For more information, see Content Type Change Control.

Place code to update your content type in the FeatureActivated(SPFeatureReceiverProperties) method of an SPFeatureReceiver object. After your code has completed modifications to the object that represents the content type, commit the changes to the database by calling one of the overloads of the Update() method of the object. You have the option to push changes down to derived content types. For more information, see Updating Child Content Types.

XML Used for SharePoint Content Types

You can define a content type by using XML in a Feature. For information about the schema that is used for content type definitions, see Content Type Definitions. You can add an existing content type to a list when you provision the list. For information about the content type section of the list schema, see ContentTypes Element (List).

For a simple example that shows how to use XML with content types, see Walkthrough: Create a Custom Field, Content Type, List Definition, and List Instance.

Building Block: Websites and Site Collections

Building Block: Columns and Field Types

Building Block: Workflows

Building Block: Features

More Information About SharePoint Content Types

Introduction to Content Types

Base Content Type Hierarchy

Creating Content Types

Updating Child Content Types

How to: Add a Content Type to a SharePoint List