创建内容类型

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

适用范围: SharePoint Foundation 2010

本文内容
内容类型继承
设计内容类型
使用声明性 XML 创建内容类型
使用代码创建内容类型

在 SharePoint Online 中提供

作为开发人员,您可以通过编写声明性 XML 或者服务器代码来创建内容类型。在创建将由其他人安装和激活的功能时,通常要执行此操作。因此,实际要对内容类型执行的操作不是创建内容类型,而是定义 内容类型。

最终,将安装功能并由某个用户激活功能。Microsoft SharePoint Foundation 执行您的代码或读取您的声明性 XML 并使内容类型可用。具有管理列表权限的管理用户将新内容类型应用于列表或文档库,内容类型将发挥作用。有权向列表添加项目的任何用户都可以使用您的内容类型创建内容。管理用户通过添加或删除列、或更改其他属性,可以根据需要修改内容类型。所有这些都不在您的控制范围内。

关键点在于,"创建"内容类型实际上是指创建内容类型定义。良好的内容类型定义可以预见内容类型的最终使用方式。

内容类型继承

您无需从头开始创建内容类型。相反,始终可以选择现有内容类型作为创建的任何新内容类型的基础。这正是 Microsoft SharePoint Foundation 的内置内容类型所起的作用。有关随 SharePoint Foundation 提供的内容类型的层次结构的信息,请参阅基内容类型层次结构

您用于创建新内容类型的网站必须位于从其派生这一新内容类型的内容类型范围内,因此编写内容类型定义时应该考虑这一点。有关详细信息,请参阅内容类型作用域

SharePoint Foundation 支持的内容类型继承形式有限。激活定义新内容类型的功能时,为父内容类型定义的列和其他元数据将传递到子内容类型。只有这部分继承是自动实现的。

备注

在早期版本的 SharePoint Foundation 中,设置内容类型后添加到内容类型的列和元数据不会由子内容类型继承。在 SharePoint Foundation 2010 中,此行为已更改。您现在可以选择包含用户已添加到父项的列和元数据。有关详细信息,请参阅 ContentType 元素的 Inherits 属性的说明。

提供并使用内容类型后,您或者其他人可以修改父内容类型的定义,但是此类更改不会自动向下推送到派生的内容类型。这方面的继承必须通过用户界面或自定义代码手动启动。有关详细信息,请参阅更新子内容类型

然而,内容类型继承允许您像处理派生类那样处理内容类型。例如,正如您可以重写基类的属性一样,您可以修改您的内容类型从其父内容类型派生的属性。您可以添加、改变或删除列;指定其他"新建"、"编辑"或"显示"表单;指定其他文档模板等。这意味着您可以创建基内容类型,就像在开发应用程序框架的过程中创建基类一样。例如,您可以定义名为"Financial Document"的内容类型,它具有组织中的所有财务文档共有的特征。然后可以使用"Financial Document"作为新内容类型(如"Invoice"和"Purchase Order")的基础。新内容类型将从"Financial Document"继承列和其他元数据,因此您只在父内容类型上定义共有元素一次。

您可以学习 SharePoint Foundation 如何使用基内容类型的继承属性以及它如何修改这些属性,以了解如何构建自己的内容类型层次结构。最好从用户界面中的内容类型库开始。在"网站操作"菜单上,选择"网站设置"。在"网站设置"页上的"库"下,单击"网站内容类型"。在"网站内容类型"页上,单击您关注的内容类型的链接。

另一个不错的学习资源是 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES\ctype 文件夹中的文件 ctypewss.xml。此文件包含很多内置内容类型的定义。如需了解定义方面的帮助,请参阅内容类型定义

设计内容类型

有关规划内容类型的综合性指南,请参阅规划内容类型和工作流。以下是一些常规注意事项:

  • 范围。您希望在网站层次结构的什么位置提供内容类型?

    如果应在整个网站集内提供内容类型,则应该在具有网站集范围的功能中部署内容类型,并在功能接收器类的 FeatureActivated 方法中编写代码,以将内容类型添加到根网站的内容类型集合中。

  • 父项。您将选择从范围内的哪个现有内容类型派生您的内容类型?

    如果在用于创建您的内容类型的网站中父内容类型不可用,则您的内容类型将不会正确激活。

  • 。哪些现有网站栏可供您使用?

    必须添加未从父内容类型继承的列。必须创建不存在的列。

  • 其他资源。内容类型是否需要语言资源、文档模板或表单?

    如果内容类型需要任何上述资源,您必须了解如何设置这些资源以及内容类型将如何访问这些资源。

使用声明性 XML 创建内容类型

创建内容类型的一种方法是使用声明性 XML 在功能的元素清单文件中定义内容类型。激活内容时,将创建内容类型。有关详细信息,请参阅内容类型定义使用功能

您可以在同一元素清单文件中定义多个内容类型。定义的每个内容类型包含一个 ContentType 元素。ID 属性是必需的,并且您指定的值必须遵循特定的格式。格式正确的内容类型 ID 不仅标识新内容类型,还标识父内容类型。有关详细信息,请参阅内容类型 ID

要为派生自内置内容类型的内容类型创建 ID,请首先确定标识该内置内容类型的十六进制字符串,追加两个零,然后添加一个移除了所有标点的 GUID。例如,标识"项"内容类型的十六进制字符串为"0x01"。派生自项的内容类型的 ID 应如下所示:

0x0100A33D9AD9805788419BDAAC2CCB37509F

要为从内容类型继承的其他内容类型创建 ID,则追加"01"、"02"等。

ContentType 元素必需的另一属性为 Name,该属性指定显示名称。该值可以为对 $Resources:String 格式的资源的引用。有关详细信息,请参阅本地化 SharePoint 解决方案(该链接可能指向英文页面)。名称本身的长度不能超过 128 个字符,并且不能包含 \ / : * ? " # % < > { } | ~ & 字符、两个连续的句点 (..) 或制表符等特殊字符。

在内容类型中包含列时,并不在内容类型定义中创建它们。实际上,您是引用 已在其他位置创建的列。若要包含列引用,请添加 FieldRef 元素作为 ContentType 元素的子项。引用的列可以是已经作为网站栏存在的列,也可以是创建内容类型的同一功能创建的新列。有关创建网站栏的信息,请参阅字段定义

还可以使用 FieldRef 元素引用内容类型从其父内容类型继承的列。在这种情况下,引用列不是要添加列,而是要更改它在内容类型中使用时的一些特征。

FieldRef 元素的 ID 属性标识要引用的列。值应该为 GUID 的字符串表示形式,包括大括号。值区分大小写。

提示提示

可以在位于以下路径的 fieldswss.xml 文件中查找内置字段的 ID:%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES\fields。

如果您的内容类型对新内容使用某一文档模板,可以在 ContentType 元素内的 DocumentTemplate 元素中标识该文档模板。必须单独设置文件。一种方法是将向清单添加 Module 元素并将文件包含在功能中。有关详细信息,请参阅如何:设置文件

内容类型定义还可以通过 XMLDocuments 元素包含自定义表单的定义。有关详细信息,请参阅内容类型中的自定义信息

示例

下面的示例显示某一功能的元素清单,它创建四个网站栏和三个网站内容类型。第一个内容类型 Financial Document 是内置 Document 内容类型的子项。其余两个新内容类型 Invoice 和 Purchase Order 都从 Financial Document 派生。

Financial Document 的定义引用两个新网站栏 DateOpened 和 Amount。引用 DateOpened 栏的 FieldRef 元素设置了 DisplayName 属性,以便栏名将呈现为"Date",而不是网站栏中定义的"Date Opened"。

Invoice 内容类型是从 Financial Document 派生的两个新内容类型中的第一个。该内容类型的定义引用第三个新网站栏 Client,并将 Required 属性设置为 TRUE,以便创建新项目时需要此字段的一个条目。Invoice 类型包含对应 Title 列的 FieldRef 元素,尽管该列是继承列,但是这样做可以将 DisplayName 属性的值从"Title"更改为"Service"。

从 Financial Document 派生的第二个内容类型是 Purchase Order。该内容类型的定义引用第四个新网站栏 CostCenter,并设置 DisplayName 属性,以便列名将呈现为"Department",而不是网站栏中定义的"Cost Center"。

Purchase Order 从其父项 Financial Document 继承列引用,因此您不必再次引用它们。该内容类型还从 Financial Document 继承对 Title 列的引用,Financial Document 从其父项 Document 继承该列。Purchase Order 类型包含对应 Title 列的 FieldRef 元素,以使用自己的值重写继承的 DisplayName 属性。

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

  <!-- Document Templates -->
  
  <Module Name="InvoiceDocumentTemplate"
          Path="FinancialDocuments"
          Url="_cts/Invoice" RootWebOnly="TRUE">
    <File Url="Invoice.docx" Type="Ghostable" />
  </Module>
  
  <Module Name="PurchaseOrderDocumentTemplate"
          Path="FinancialDocuments"
          Url="_cts/PurchaseOrder" RootWebOnly="TRUE">
    <File Url="PurchaseOrder.docx" Type="Ghostable" />
  </Module>


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

   <Field ID="{86811853-7E52-4515-A88D-A8FA9D450905}"
         Name="Client"
         DisplayName="Client Name"
         Type="Text"
         Required="FALSE"
         Group="Financial Columns"/>

    <Field ID="{1511BF28-A787-4061-B2E1-71F64CC93FD5}"
         Name="DateOpened"
         DisplayName="Date Opened"
         Type="DateTime"
         Format="DateOnly"
         Required="FALSE"
         Group="Financial Columns">
    <Default>[today]</Default>
  </Field>
  
 <Field ID="{943E7530-5E2B-4C02-8259-CCD93A9ECB18}"
         Name="CostCenter"
         DisplayName="Department"
         Type="Choice"
         Required="FALSE"
         Group="Financial Columns">
    <CHOICES>
      <CHOICE>Administration</CHOICE>
      <CHOICE>Information</CHOICE>
      <CHOICE>Facilities</CHOICE>
      <CHOICE>Operations</CHOICE>
      <CHOICE>Sales</CHOICE>
      <CHOICE>Marketing</CHOICE>
    </CHOICES>
  </Field>

  <!-- Site Content Types -->
  
  <!-- Parent ContentType: Document (0x0101) -->
  <ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e"
               Name="Financial Document"
               Group="Financial Content Types"
               Description="Base financial content type"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{1511BF28-A787-4061-B2E1-71F64CC93FD5}" Name="DateOpened" DisplayName="Date" Required="TRUE"/>
      <FieldRef ID="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}" Name="Amount" DisplayName="Amount" Required="FALSE"/>
    </FieldRefs>
  </ContentType>

  <!-- Parent ContentType: Financial Document -->
  <ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e01"
                Name="Invoice"
                Group="Financial Content Types"
                Description="Used for creating customer invoices"
                Inherits="TRUE"
                Version="0">
    <FieldRefs>
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Service" Required="TRUE" Sealed="TRUE"/>
      <FieldRef ID="{86811853-7E52-4515-A88D-A8FA9D450905}" Name="Client" DisplayName="Client Name" Required="TRUE"/>
    </FieldRefs>
    <DocumentTemplate TargetName="Invoice.docx" />
  </ContentType>
  
  <!-- Parent ContentType: Financial Document -->
  <ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e02"
               Name="PurchaseOrder"
               Group="Financial Content Types"
               Description="Used for creating purchase orders"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Item" Required="TRUE" Sealed="TRUE"/>
      <FieldRef ID="{943E7530-5E2B-4C02-8259-CCD93A9ECB18}" Name="CostCenter" DisplayName="Department" Required="TRUE"/>
    </FieldRefs>
    <DocumentTemplate TargetName="PurchaseOrder.docx"/>
  </ContentType>

</Elements>

使用代码创建内容类型

除了可以使用声明性 XML 创建内容类型外,还可以使用 SharePoint Foundation 对象模型。通常,声明性 XML 更易于编写。但是,声明性 XML 在灵活性方面通常不如对象模型,后者可以访问整个 Microsoft .NET Framework 的功能并且可以在运行时进行调试。两种方法都受随 Microsoft Visual Studio 2010 中的 SharePoint 开发工具提供的模板支持。

将定义内容类型的代码放在 SPFeatureReceiver 类的子类的 FeatureActivated 方法中。

可以通过调用 SPContentType 类的构造函数创建内容类型。在调用构造函数之前,必须收集三个项目以作为参数传递:

  • 一个 SPContentType 对象,表示父内容类型。

    可以从 SPWeb 对象(表示用于创建内容类型的网站)的 AvailableContentTypes 属性返回的集合中检索此对象。

  • 一个 SPContentTypeCollection 对象,表示用于创建内容类型的网站的集合。

    可以通过访问 SPWeb 对象(表示用于创建内容类型的网站)的 ContentTypes 属性获取此对象。

  • 一个字符串,包含要提供给内容类型的名称。

    您提供的名称将用作内容类型的显示名称。一些字符不允许在内容类型名称中使用。可以通过调用静态方法 ValidateName(String) 验证建议的名称。此方法检查非法字符,但是不检查重复名称。

调用构造函数以创建新 SPContentType 对象后,必须通过单独调用 SPContentTypeCollection 对象(表示网站的集合)的 Add(SPContentType) 方法将其添加到网站的内容类型集合中。此步骤完成内容类型的初始化。

警告注释警告

如果集合中已经存在同名内容类型,则 Add(SPContentType) 方法将引发 SPException 异常。

如果要设置内容类型的属性,应该保存由 Add 方法返回的 SPContentType 对象。

可以通过在 SPFieldLink 对象中引用每个列来将列添加到内容类型。然后调用由内容类型的 FieldLinks 属性返回的 SPFieldLinkCollection 对象的 Add(SPFieldLink) 方法。

完成内容类型的配置后,调用 Update() 方法以将更改提交到数据库。

示例

下面的示例创建的网站栏和内容类型与前面的示例创建的完全相同。唯一的区别是本示例使用功能接收器的 FeatureActivated 方法中的代码,而不是使用声明性 XML。

FeatureActivated 方法的 properties 参数是 SPFeatureReceiverProperties 对象。此对象的 Feature 属性返回的 SPFeature 对象具有包含封装的 SPSite 对象或封装的 SPWeb 对象的 Parent 属性。您不应该释放这些对象。但是,应该释放代码创建的任何新 SPWebSPSite 对象。有关详细信息,请参阅释放对象

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = null;
    if (properties.Feature.Parent is SPSite)
    {
        SPSite sites = (SPSite)properties.Feature.Parent;
        web = sites.RootWeb;
    }
    else
    {
        web = (SPWeb)properties.Feature.Parent;
    }
    if (web == null)
        return;

    /* CREATE SITE COLUMNS */

    string columnGroup = "Financial Columns";

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

    // Client Name
    string clientFieldName = web.Fields.Add("Client Name", SPFieldType.Text, false);
    SPFieldText clientField = (SPFieldText)web.Fields.GetFieldByInternalName(clientFieldName);
    clientField.Group = columnGroup;
    clientField.Update();

    // Date Opened
    string dateOpenedFieldName = web.Fields.Add("Date Opened", SPFieldType.DateTime, false);
    SPFieldDateTime dateOpenedField = (SPFieldDateTime)web.Fields.GetFieldByInternalName(dateOpenedFieldName);
    dateOpenedField.Group = columnGroup;
    dateOpenedField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
    dateOpenedField.DefaultValue = "[today]";
    dateOpenedField.Update();

    // Cost Center Name
    string costCenterFieldName = web.Fields.Add("Cost Center", SPFieldType.Choice, false);
    SPFieldChoice costCenterField = (SPFieldChoice)web.Fields.GetFieldByInternalName(costCenterFieldName);
    costCenterField.Choices.Add("Administration");
    costCenterField.Choices.Add("Information Services");
    costCenterField.Choices.Add("Facilities");
    costCenterField.Choices.Add("Operations");
    costCenterField.Choices.Add("Sales");
    costCenterField.Choices.Add("Marketing");
    costCenterField.Group = columnGroup;
    costCenterField.Update();

    /* CREATE SITE CONTENT TYPES */

    string contentTypeGroup = "Financial Content Types";

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

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

    // Note: A content type is not initialized until after it is added.
    financialDocumentCType = web.ContentTypes.Add(financialDocumentCType);
    financialDocumentCType.Group = contentTypeGroup;

    // Add the Date Opened column. Child content types inherit the column.
    SPFieldLink dateOpenedFieldRef = new SPFieldLink(dateOpenedField);
    dateOpenedFieldRef.Required = true;
    financialDocumentCType.FieldLinks.Add(dateOpenedFieldRef);

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

    // Commit changes.
    financialDocumentCType.Update();

    // Create the Invoice content type.
    SPContentType invoiceCType = new SPContentType(financialDocumentCType, web.ContentTypes, "Invoice");
    invoiceCType = web.ContentTypes.Add(invoiceCType);
    invoiceCType.Group = contentTypeGroup;

    // Modify the Title column inherited from the parent.
    SPFieldLink serviceFieldRef = invoiceCType.FieldLinks[SPBuiltInFieldId.Title];
    serviceFieldRef.DisplayName = "Service";
    serviceFieldRef.Required = true;

    // Add the Client column.
    SPFieldLink clientFieldRef = new SPFieldLink(clientField);
    clientFieldRef.Required = true;
    invoiceCType.FieldLinks.Add(clientFieldRef);

    // Specify a document template.
    invoiceCType.DocumentTemplate = "Invoice.docx";

    // Commit changes.
    invoiceCType.Update();

    // Create the Purchase Order content type.
    SPContentType purchaseOrderCType = new SPContentType(financialDocumentCType, web.ContentTypes, "Purchase Order");
    purchaseOrderCType = web.ContentTypes.Add(purchaseOrderCType);
    purchaseOrderCType.Group = contentTypeGroup;

    // Modify the Title column inherited from the parent.
    SPFieldLink itemFieldRef = purchaseOrderCType.FieldLinks[SPBuiltInFieldId.Title];
    itemFieldRef.DisplayName = "Item";
    itemFieldRef.Required = true;

    // Add the Department column.
    SPFieldLink departmentFieldRef = new SPFieldLink(costCenterField);
    departmentFieldRef.DisplayName = "Department";
    departmentFieldRef.Required = true;
    purchaseOrderCType.FieldLinks.Add(departmentFieldRef);

    // Specify a document template.
    purchaseOrderCType.DocumentTemplate = "PurchaseOrder.docx";

    // Commit changes.
    purchaseOrderCType.Update();

}
Public Overrides Sub FeatureActivated(ByVal properties As Microsoft.SharePoint.SPFeatureReceiverProperties)
    Dim web As SPWeb = Nothing
    If TypeOf properties.Feature.Parent Is SPSite Then
        Dim sites As SPSite = DirectCast(properties.Feature.Parent, SPSite)
        web = sites.RootWeb
    Else
        web = DirectCast(properties.Feature.Parent, SPWeb)
    End If
    If web Is Nothing Then
        Return
    End If

    ' CREATE SITE COLUMNS 

    Dim columnGroup As String = "Financial Columns"

    ' Amount
    Dim amountFieldName As String = web.Fields.Add("Amount", SPFieldType.Currency, False)
    Dim amountField As SPFieldCurrency = DirectCast(web.Fields.GetFieldByInternalName(amountFieldName), SPFieldCurrency)
    amountField.Group = columnGroup
    amountField.DisplayFormat = SPNumberFormatTypes.TwoDecimals
    amountField.MinimumValue = 0
    amountField.Update()

    ' Client Name
    Dim clientFieldName As String = web.Fields.Add("Client Name", SPFieldType.Text, False)
    Dim clientField As SPFieldText = DirectCast(web.Fields.GetFieldByInternalName(clientFieldName), SPFieldText)
    clientField.Group = columnGroup
    clientField.Update()

    ' Date Opened
    Dim dateOpenedFieldName As String = web.Fields.Add("Date Opened", SPFieldType.DateTime, False)
    Dim dateOpenedField As SPFieldDateTime = DirectCast(web.Fields.GetFieldByInternalName(dateOpenedFieldName), SPFieldDateTime)
    dateOpenedField.Group = columnGroup
    dateOpenedField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly
    dateOpenedField.DefaultValue = "[today]"
    dateOpenedField.Update()

    ' Cost Center Name
    Dim costCenterFieldName As String = web.Fields.Add("Cost Center", SPFieldType.Choice, False)
    Dim costCenterField As SPFieldChoice = DirectCast(web.Fields.GetFieldByInternalName(costCenterFieldName), SPFieldChoice)
    costCenterField.Choices.Add("Administration")
    costCenterField.Choices.Add("Information Services")
    costCenterField.Choices.Add("Facilities")
    costCenterField.Choices.Add("Operations")
    costCenterField.Choices.Add("Sales")
    costCenterField.Choices.Add("Marketing")
    costCenterField.Group = columnGroup
    costCenterField.Update()

    ' CREATE SITE CONTENT TYPES 

    Dim contentTypeGroup As String = "Financial Content Types"

    ' Get a content type to be the parent of a new Financial Document content type.
    Dim documentCType As SPContentType = web.AvailableContentTypes(SPBuiltInContentTypeId.Document)

    ' Create the Financial Document content type.
    Dim financialDocumentCType As New SPContentType(documentCType, web.ContentTypes, "Financial Document")

    ' Note: A content type is not initialized until after it is added.
    financialDocumentCType = web.ContentTypes.Add(financialDocumentCType)
    financialDocumentCType.Group = contentTypeGroup

    ' Add the Date Opened column. Child content types inherit the column.
    Dim dateOpenedFieldRef As New SPFieldLink(dateOpenedField)
    dateOpenedFieldRef.Required = True
    financialDocumentCType.FieldLinks.Add(dateOpenedFieldRef)

    ' Add the Amount column. Child content types inherit the column.
    Dim amountFieldRef As New SPFieldLink(amountField)
    financialDocumentCType.FieldLinks.Add(amountFieldRef)

    ' Commit changes.
    financialDocumentCType.Update()

    ' Create the Invoice content type.
    Dim invoiceCType As New SPContentType(financialDocumentCType, web.ContentTypes, "Invoice")
    invoiceCType = web.ContentTypes.Add(invoiceCType)
    invoiceCType.Group = contentTypeGroup

    ' Modify the Title column inherited from the parent.
    Dim serviceFieldRef As SPFieldLink = invoiceCType.FieldLinks(SPBuiltInFieldId.Title)
    serviceFieldRef.DisplayName = "Service"
    serviceFieldRef.Required = True

    ' Add the Client column.
    Dim clientFieldRef As New SPFieldLink(clientField)
    clientFieldRef.Required = True
    invoiceCType.FieldLinks.Add(clientFieldRef)

    ' Specify a document template.
    invoiceCType.DocumentTemplate = "Invoice.docx"

    ' Commit changes.
    invoiceCType.Update()

    ' Create the Purchase Order content type.
    Dim purchaseOrderCType As New SPContentType(financialDocumentCType, web.ContentTypes, "Purchase Order")
    purchaseOrderCType = web.ContentTypes.Add(purchaseOrderCType)
    purchaseOrderCType.Group = contentTypeGroup

    ' Modify the Title column inherited from the parent.
    Dim itemFieldRef As SPFieldLink = purchaseOrderCType.FieldLinks(SPBuiltInFieldId.Title)
    itemFieldRef.DisplayName = "Item"
    itemFieldRef.Required = True

    ' Add the Department column.
    Dim departmentFieldRef As New SPFieldLink(costCenterField)
    departmentFieldRef.DisplayName = "Department"
    departmentFieldRef.Required = True
    purchaseOrderCType.FieldLinks.Add(departmentFieldRef)

    ' Specify a document template.
    purchaseOrderCType.DocumentTemplate = "PurchaseOrder.docx"

    ' Commit changes.
    purchaseOrderCType.Update()

End Sub

请参阅

任务

如何:引用内容类型中的列

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

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

概念

网站和列表内容类型

内容类型作用域

内容类型定义

其他资源

规划内容类型和工作流

在 SharePoint 2010 中创建自定义内容类型

演练:创建自定义字段、内容类型、列表定义和列表实例

如何:以编程方式创建内容类型(该链接可能指向英文页面)