如何:向网站添加内容类型

上次修改时间: 2010年11月1日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

可以指定要包含在自定义网站定义配置中的内容类型,以便在用户创建此类型的网站时,Microsoft SharePoint Foundation 默认情况下会使这些内容类型可供网站使用。创建网站后,可添加作为功能的一部分的内容类型。

在网站定义配置中注册内容类型

为指定要包含在自定义网站定义配置中的内容类型,可先创建作为单独功能的内容类型,然后通过某些协作应用程序标记语言 (CAML) 标记对网站定义的 Onet.xml 文件中的网站定义配置引用该功能。这样,当用户创建该类型的新网站时,默认情况下会将该内容类型包括在网站的内容类型集合中。

警告注释警告

不支持为 SharePoint Foundation 的任何内置网站类型修改 Onet.xml 文件,因此下列过程只能用于自定义的网站类型。有关创建自定义网站定义配置的详细信息,请参阅如何:创建自定义网站定义和配置

在网站定义配置中指定内容类型

  1. 创建作为单独的功能的内容类型。

    有关详细信息,请参阅使用功能

  2. 在用于定义您的自定义网站类型的 Configuration 元素(在 Onet.xml 文件中)中引用该功能:

    1. 打开 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates\site_type\XML 中的 Onet.xml 文件,其中 site_type 是自定义网站定义的名称。

    2. 在 Configuration 元素中,添加对 SiteFeatures 或 WebFeatures 元素中内容类型功能的引用。可以通过功能的 GUID 标识各个功能。下面是一个示例。

      <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>
      

      有关这两个元素的差异的详细信息,请参阅 SiteFeatures 元素(网站)WebFeatures 元素(网站)

向现有网站添加内容类型

可使用声明 XML 或 SharePoint Foundation 对象模型向网站中添加内容类型。声明 XML 使用内容类型定义架构来定义内容类型。内容类型定义在功能的元素清单文件中声明,并且当激活功能时,会将内容类型添加到网站中。

当使用 SharePoint Foundation 对象模型时,必须设置 SPFeatureReceiver 类的子类。将创建内容类型并将这些内容类型添加到网站中的代码置于 FeatureActivated 方法中。

通常情况下,声明 XML 更易于编写,但其灵活性不如对象模型,后者可访问整个 Microsoft .NET Framework 的功能,并可在运行时调试。这两种方法都受随 Microsoft Visual Studio 2010 中的 SharePoint 开发工具提供的模板的支持。

使用声明 XML 添加内容类型

  1. 创建功能。

    有关详细信息,请参阅使用功能

  2. 向功能中添加元素清单文件。

  3. 在元素清单中添加作为根 XML 元素的 Elements 元素。

  4. 添加作为 Elements 元素的子级的 ContentType 元素。

    备注

    用于 ID 属性的值具有特定格式。有关详细信息,请参阅内容类型 ID

  5. 根据需要添加列引用。有关详细信息,请参阅如何:引用内容类型中的列

  6. 根据需要添加 DocumentTemplate 定义。

    备注

    DocumentTemplate 元素引用的文件必须已位于网站上,或必须包含在功能中。有关详细信息,请参阅如何:设置文件

  7. 根据需要完成内容类型定义。有关详细信息,请参阅内容类型定义

示例

下面的示例是功能的元素清单。激活该功能时,可使用该元素清单创建网站栏并将其添加到当前网站上的网站栏集合中。然后,使用清单创建网站内容类型并将其添加到当前网站上的内容类型集合中。

<?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>

使用服务器代码添加内容类型

  1. 创建一个从 SPFeatureReceiver 类继承的功能接收器类。

  2. FeatureActivated 方法中,使用 ContentTypes 属性访问网站的内容类型集合。

    此属性返回一个 SPContentTypeCollection 对象。

  3. 创建 SPContentType 对象。有关详细信息,请参阅创建内容类型

  4. 使用 Add 方法可向网站内容类型集合添加 SPContentType 对象。

示例

以下示例显示 SPFeatureReceiver 类的子类中 FeatureActivated 方法的实现。激活功能时,FeatureActivated 方法中的代码会创建一个网站栏,并将其添加到当前网站上的网站栏集合中。然后代码会创建一个从文档内容类型继承的内容类型,并将其添加到当前网站上的内容类型集合中。

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

请参阅

任务

如何:向列表添加内容类型

概念

内容类型简介

网站和列表内容类型

内容类型定义