SharePoint 2007 Deployment: Site Content Type Features

Summary

This post is about developing features to create site content types in a SharePoint site. Check the first post SharePoint 2007 Deployment: Overview for an introduction and the series index.

Package Structure

As I mentioned previously in the post SharePoint 2007 Deployment: Creating and Using Features, to build a feature you need to create the following files:

  • The feature manifest file (which must be named feature.xml)
  • One or more element manifest files

The feature manifest file contains the generic information about the feature package, and the element manifest files contain the information about each specific type of element that makes up the feature. Since I already explained all the possible contents of the feature manifest file in the above mentioned post, I will focus this one the element manifest that allows the creation of site content types in a SharePoint site.

You can then place these two files inside a Solution following the instructions in the post SharePoint 2007 Deployment: Creating Solutions, to provide an easy way to deploy the feature (or upgrade it).

Site Content Types

A Content Type is a definition of the metadata and behavior of a SharePoint list item. In short, it specifies a type of list item based on:

  • The columns that compose the list item;
  • The document template used to create a new item (if it’s a document content type);
  • The information management policies associated with it;
  • The workflows associated with it.

Content types have a hierarchic structure, which means that each content type has a parent content type from which it inherits all the definitions. These definitions can be overriden in the child content type.

Each list can have one or more associated content types. Each content type associated to a specific list is called List Content Type. However, any List Content Type must be based on a Site Content Type, which is a content type definition that is not associated to a specific list.

A Site Content Type exists in the context of a web site (organized in groups, in the site content type gallery) and can be associated with any lists of that web site or child web sites.

One can create new site content types or edit the existing ones following the steps below:

  • Site Actions (press the button)
  • Site Settings
  • Site Content Types (in the Galleries option group)

Allowed Scopes

The scopes to which a feature can be deployed, are dictated by the types of elements included in it. A feature with site content type elements can only be deployed to a Site Collection scope, even though you can create a site content type in a non-root web site.

Attempting to install a feature of another scope that contains site content type elements, will result in an error.

Feature Manifest

I will only present a simple feature manifest, since the additional options were presented in the above mentioned post.

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
         Id="5DFD12AF-D0AA-4c63-8FB8-C49DB1191083"
         Title="My Site Content Type Feature"
         Description="Adds my Content Types."
         Scope="Site"
         Version="1.0.0.0">
    <ElementManifests>
        <ElementManifest Location="ContentTypes.xml"/>
    </ElementManifests>
</Feature>

Notes about this feature manifest:

  • The title of the feature is My Site Content Type Feature.
  • It will be deployed as a Site Collection feature, since it’s Scope value is Site.
  • It references a single element manifest file: ContentTypes.xml.

Element Manifest

The element manifest file can have any name you wish (in this example it’s called ContentTypes.xml), but it’s root element must be <Elements>. Inside this root element, you can place any number of feature element descriptions. In this example I will present the use of the <ContentType> element which is used to deploy Site Content Types.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <ContentType
      ID="0x0100C5647A362F236548B218C15302286758"
      Name="My Content Type"
      Description="Simple Custom Content Type"
      ReadOnly="FALSE"
      Hidden="FALSE"
      Sealed="FALSE"
      FeatureId="5DFD12AF-D0AA-4c63-8FB8-C49DB1191083"
      Group="Custom Content Types">
      <FieldRefs>
         <RemoveFieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" 
Name="Title"/> <FieldRef ID="{b402db15-ee44-4ec4-89e3-23e10a8fc64c}"
Name="My_x0200_Field" /> <FieldRef ID="{538c71e4-8650-4ce7-b021-920effa66346}"
Name="Status" /> </FieldRefs> </ContentType> </Elements>

This example creates a site content type named My Content Type with two custom site columns. Here is a short explanation of the attributes of the <ContentType> element:

  • ID – The ID of the new content type. This ID bears information about the content type hierarchy and, for that reason, cannot be randomly generated. More about this below.
  • Name – The name of the content type.
  • Description – (optional) A descriptive text about the content type.
  • ReadOnly – (optional) Specifies if the content type is read-only, which means its definition cannot be changed. The default value is FALSE.
  • Hidden – (optional) Specifies if the content type is hidden, which means SharePoint will not show it in the New button in the list toolbar. The default value is FALSE.
  • Sealed – (optional) Specifies if the content type is sealed, which means that it cannot have child content types. Only a site collection administrator can unseal a content type. The default value is FALSE.
  • FeatureId – (optional) ID of the feature in which the content type is included.
  • Group – (optional) Name of the content type group to which this content type is assigned.

The <ContentType> element has three optional child elements, one of which is shown in the sample above: the <FieldRefs> element. The other two are <XmlDocuments> and <DocumentTemplate>.

The FieldRefs Element

The <FieldRefs> element is used to specify which site columns are part of the site content type. This element has two possible child elements: <FieldRef> and <RemoveFieldRef>.

The <FieldRef> element specifies a site column that belongs to the content type, and <RemoveFieldRef> specifies a site column that belongs to the parent content type but will be removed from this content type. Both these elements have a mandatory ID attribute that uniquely identify the site column and can have several additional optional attributes (which are a subset of those presented in the previous post for the <Field>; element).

The XmlDocuments Element

Although not used very often, this element allows you to include custom information in the content type in the form of XML nodes. These nodes are represented by a <XmlDocument> element placed inside the <XmlDocuments> element. The contents of the <XmlDocument> element can conform to any schema, sufficing that they are valid XML.

The DocumentTemplate Element

This element is only used if you’re defining a document content type. It allows you to specify the path to the document template that is used to create new items of this content type in a document library. It has a single attribute, TargetName, which is the path (either absolute or relative) to the document template.

Content Type IDs

The content type ID uniquely identifies a content type and encapsulates the content type’s hierarchy. That is, the ID of a certain content type includes the ID of its parent content type.

To create a new ID for a content type you can follow one of two possible conventions:

  • [parent content type ID] + two hexadecimal values (different from "00")
  • [parent content type ID] + "00" + hexadecimal GUID

Usually the first convention is used by SharePoint for the default content types included in SharePoint.

Example:

  • 0x (System)
  • 0x01 (Item Content Type)
  • 0x0101 (Document Content Type, child of Item Content Type)
  • 0x0120 (Folder Content Type, child of Item Content Type)

The second convention is used by SharePoint when

  • You create a new content type based on an existing one
  • A site content type is copied to a list (becoming a list content type)

You should use this convention whenever you create a new content type based on a content type you didn’t create. If you’re creating a content type based on another content type you have create, then you can use the first approach.

Example:

  • 0x01 (Item Content Type)
  • 0x0100C5647A362F236548B218C15302286758 (Custom Content Type based on the Item Content Type)
  • 0x0100C5647A362F236548B218C1530228675801 (Custom Content Type based on the previous content type)

One final note about Content Type IDs is that they have a maximum length of 512 bytes which can hold 1024 hexadecimal characters. The above conventions will help you keep below this limit while ensuring that no one creates a new content type with the same ID.