Partilhar via


How to: Add a Content Type to a Site

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

You can specify content types to include in a custom site definition configuration so that each time a user creates a site of that type, Microsoft SharePoint Foundation makes the content types available on the site by default. After a site is created, you can add content types as part of a Feature.

Registering Content Types in a Site Definition Configuration

To specify a content type to include in a custom site definition configuration, you first create the content type as a separate Feature, and then you reference that Feature in some Collaborative Application Markup Language (CAML) markup to the site definition configuration in the Onet.xml file of the site definition. Then, when a user creates a new site of that type, the content type is included by default in the site’s content type collection.

Warning

Modifying the Onet.xml file for any of the built-in site types of SharePoint Foundation is not supported, so you can use the following procedure only on custom site types. For more information about creating custom site definition configurations, see How to: Create a Custom Site Definition and Configuration.

To specify a content type in a site definition configuration

  1. Create the content type as a separate Feature.

    For more information, see Using Features in SharePoint Foundation.

  2. Reference that Feature in the Configuration element (in the Onet.xml file) that defines your custom site type:

    1. Open the Onet.xml file in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates\site_type\XML, where site_type is name of the custom site definition.

    2. In the Configuration element, add a reference to the content type Feature in either the SiteFeatures or WebFeatures element. You identify the Feature by its GUID. The following is an example.

      <SiteFeatures>
        <Feature ID="00BFEA71-1C5E-4A24-B310-BA51C3EB7A57" />
        <Feature ID="695B6570-ACDC-4A8E-8545-26EA7FC1D162" />
      </SiteFeatures>
      <WebFeatures>
        <Feature ID="00BFEA71-4EA5-48D4-A4AD-7EA5C011ABE5" />
        <Feature ID="00BFEA71-E717-4E80-DEAF-D0C71B360101" />
      </WebFeatures>
      

      For more information about the difference between these two elements, see SiteFeatures Element (Site) and WebFeatures Element (Site).

Adding Content Types to an Existing Site

You can add content types to a site by using either declarative XML or the SharePoint Foundation object model. Declarative XML uses the Content Type Definition Schema to define content types. Content type definitions are declared in the element manifest file of a Feature, and the content types are added to a site when the Feature is activated.

When you use the SharePoint Foundation object model, you must subclass the SPFeatureReceiver class. Place the code that creates your content types and adds them to the site in the FeatureActivated method.

Typically, declarative XML is easier to write. However, declarative XML generally offers less flexibility than the object model, which has access to the capabilities of the entire Microsoft .NET Framework and can be debugged at run time. Both approaches are supported by templates that are provided with SharePoint development tools in Microsoft Visual Studio 2010.

To add a content type by using declarative XML

  1. Create a Feature.

    For more information, see Using Features in SharePoint Foundation.

  2. Add an element manifest file to the Feature.

  3. Add an Elements element as the root XML element in the element manifest.

  4. Add a ContentType element as the child of the Elements element.

    Note

    The value that you use for the ID attribute has a very specific format. For more information, see Content Type IDs.

  5. Add the column references that you want. For more information, see How to: Reference a Column in a Content Type.

  6. Add a DocumentTemplate definition as desired.

    Note

    The file that is referenced by the DocumentTemplate element must already exist on the site or must be included with the Feature. For more information, see How to: Provision a File.

  7. Complete the content type definition as desired. For more information, see Content Type Definitions.

Example

The following example is the element manifest for a Feature. When the Feature is activated, the element manifest is used to create a site column and add it to the site column collection on the current site. Then the manifest is used to create a site content type and add it to the content type collection on the current site.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">

  <!-- Create a site column. -->

  <Field ID="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}"
         Name="Amount"
         DisplayName="Amount"
         Type="Currency"
         Decimals="2"
         Min="0"
         Required="FALSE"
         Group="Financial Columns" />

  <!-- Create a site content type that uses the column. -->

  <!-- Parent ContentType: Document (0x0101) -->
  <ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e"
               Name="Financial Document"
               Group="Financial Content Types"
               Description="Base financial content type"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}" Name="Amount" DisplayName="Amount" Required="FALSE"/>
    </FieldRefs>
  </ContentType>

</Elements>

To add a content type by using server code

  1. Create a feature receiver class that inherits from the SPFeatureReceiver class.

  2. In the FeatureActivated method, use the ContentTypes property to access the content type collection for the site.

    This property returns an SPContentTypeCollection object.

  3. Create an SPContentType object. For more information, see Creating Content Types.

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

Example

The following example shows the implementation of the FeatureActivated method in a subclass of the SPFeatureReceiver class. When the Feature is activated, code in the FeatureActivated method creates a site column and adds it to the site column collection on the current site. Then the code creates a content type that inherits from the Document content type and adds it to the content type collection on the current site.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite siteCollection = (SPSite)properties.Feature.Parent;
    SPWeb site = siteCollection.OpenWeb();

    /* Create a site column. */

    string amountFieldName = site.Fields.Add("Amount", SPFieldType.Currency, false);
    SPFieldCurrency amountField = (SPFieldCurrency)site.Fields.GetFieldByInternalName(amountFieldName);
    amountField.Group = "Financial Columns";
    amountField.DisplayFormat = SPNumberFormatTypes.TwoDecimals;
    amountField.MinimumValue = 0;
    amountField.Update();

    /* Create a site content type that uses the column. */

    // Get a content type to be the parent of a new Financial Document content type.
    SPContentType documentCType = site.AvailableContentTypes[SPBuiltInContentTypeId.Document];

    // Create the Financial Document content type.
    SPContentType financialDocumentCType = new SPContentType(documentCType, site.ContentTypes, "Financial Document");
    site.ContentTypes.Add(financialDocumentCType);

    // Note: A content type is not initialized until after it is added.
    financialDocumentCType = site.ContentTypes[financialDocumentCType.Id];
    financialDocumentCType.Group = "Financial Content Types";

    // Add the Amount column. Child content types inherit the column.
    SPFieldLink amountFieldRef = new SPFieldLink(amountField);
    financialDocumentCType.FieldLinks.Add(amountFieldRef);

    // Commit changes.
    financialDocumentCType.Update();

    site.Dispose();
    siteCollection.Dispose();
}
Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties)
    Dim siteCollection As SPSite = DirectCast(properties.Feature.Parent, SPSite)
    Dim site As SPWeb = siteCollection.OpenWeb()
    
    ' Create a site column. 
    
    
    Dim amountFieldName As String = site.Fields.Add("Amount", SPFieldType.Currency, False)
    Dim amountField As SPFieldCurrency = DirectCast(site.Fields.GetFieldByInternalName(amountFieldName), SPFieldCurrency)
    amountField.Group = "Financial Columns"
    amountField.DisplayFormat = SPNumberFormatTypes.TwoDecimals
    amountField.MinimumValue = 0
    amountField.Update()
    
    ' Create a site content type. 
    
    
    ' Get a content type to be the parent of a new Financial Document content type.
    Dim documentCType As SPContentType = site.AvailableContentTypes(SPBuiltInContentTypeId.Document)
    
    ' Create the Financial Document content type.
    Dim financialDocumentCType As New SPContentType(documentCType, site.ContentTypes, "Financial Document")
    site.ContentTypes.Add(financialDocumentCType)
    
    ' Note: A content type is not initialized until after it is added.
    financialDocumentCType = site.ContentTypes(financialDocumentCType.Id)
    financialDocumentCType.Group = "Financial Content Types"
    
    ' Add the Amount column. Child content types inherit the column.
    Dim amountFieldRef As New SPFieldLink(amountField)
    financialDocumentCType.FieldLinks.Add(amountFieldRef)
    
    ' Commit changes.
    financialDocumentCType.Update()
    
    site.Dispose()
    siteCollection.Dispose()
End Sub

See Also

Tasks

How to: Add a Content Type to a SharePoint List

Concepts

Introduction to Content Types

Site and List Content Types

Content Type Definitions