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

上次修改时间: 2015年1月13日

适用范围: SharePoint Foundation 2010

列从不在内容类型中直接定义。列是在其他位置定义的,并在内容类型中引用。基于以下两个原因,在内容类型中引用列:

  • 需要向内容类型添加列。

  • 需要更改内容类型从其父内容类型继承的列的特性。

    有关继承的详细信息,请参阅内容类型定义架构中 ContentType 元素的 Inherits 属性的描述。

在声明性 XML 中,通过使用 FieldRef 元素来引用列;在代码中,通过创建 SPFieldLink 对象来执行该操作。

备注

在 Microsoft SharePoint Foundation 中,字段是列的另一种称谓。在谈到用户界面如何表示字段时,经常会使用"列"这个词。

有关列与列引用之间的差异的信息,请参阅字段和字段引用

使用声明性 XML 引用列

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

内容类型定义可通过在 FieldRef 元素中引用一个列来包含该列。引用的列可以是以网站栏形式存在的列,也可以是由创建内容类型的同一功能所创建的新列。还可以使用 FieldRef 元素引用内容类型从其父内容类型继承的列。在此情况下,您之所以引用列,不是因为要添加列,而是因为要在内容类型中使用列时更改列的某些特性。

使用声明性 XML 创建列引用

  1. 在内容类型定义中,向 FieldRefs 节点添加 FieldRef 元素。

  2. 通过将 FieldRef 元素的 ID 属性值设置为列的 ID 来引用列。

    该值必须为包含在括号中的 GUID 的字符串表示形式,如以下示例所示:

    ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}"

    由于 ID 属性区分大小写,因此请确保将该属性设置为与 Field 元素的 ID 属性完全相同的值。

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

  3. 将 FieldRef 元素的 Name 属性的值设置为与表示列的 Field 元素的 Name 属性相同的值。

  4. 若要按您的需要定义列,请设置 FieldRef 元素的其他属性。

    例如,若要更改为列显示的文本,请设置 DisplayName 属性的值。

FieldRef 元素上的许多属性都具有与引用的 Field 元素上的属性相同的名称和用途。只有 ID 属性的值和 Name 属性的值必须相同。对于其他属性,在内容类型中使用某个列时,使用 FieldRef 元素上的其他值会更改该列的某些特性,而不会更改引用的列本身。

重要注释重要信息

FieldRef 元素上的 ID、Name、DisplayName 和 Required 属性始终都是必需属性,即使 FieldRef 元素引用的是同一元素清单中定义的字段也是如此。

示例

下面的示例显示了用于创建三个网站栏和两个网站内容类型的功能的元素清单。第一个内容类型 Financial Document 是内置 Document 内容类型的子级。

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

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

  <!-- New Site 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="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}"
         Name="Amount"
         DisplayName="Amount"
         Type="Currency"
         Decimals="2"
         Min="0"
         Required="FALSE"
         Group="Financial Columns" />

  <Field ID="{943E7530-5E2B-4C02-8259-CCD93A9ECB18}"
         Name="CostCenter"
         DisplayName="Cost Center"
         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="Purchase Order"
               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>
  </ContentType>

</Elements>

在代码中引用列

作为使用声明性 XML 创建内容类型的替代方式,可以在 SPFeatureReceiver 类的子类的 FeatureActivated 方法中创建内容类型。创建内容类型的代码可包含一个现有网站栏、列表栏或是这些代码所创建的新栏,方式是使用 SPFieldLink 对象引用相应的列。和声明性 XML 的情况一样,您可能还需要引用内容类型所继承的列,以便在内容类型中更改该列的特性。

此外,在其他情况下,您可能会使用代码在内容类型中引用列。例如,您可能需要通过添加或删除列引用来升级现有内容类型。或者,您可能需要从列表定义创建一个列表,然后通过添加或删除列来修改该列表中的内容类型。无论您要达到的目的如何,使用 SharePoint Foundation 对象模型引用列的方法基本相同。

在代码中创建列引用

  1. 获取对表示要引用的字段的 SPField 对象的引用。

    可以从保留在表示网站的 SPWeb 对象的 Fields 属性中的集合检索 SPField 对象。

  2. 通过将 SPField 对象传递给 SPFieldLink 构造函数来创建用于表示列引用的 SPFieldLink 对象。

  3. 获取对 SPContentType 对象的引用。

    如果创建新的内容类型,则可使用由 SPContentType 类构造函数返回的对象。如果修改现有内容类型,则可从保留在 SPWeb 对象或 SPList 对象的 ContentTypes 属性中的集合检索 SPContentType 对象。

  4. 使用 FieldLinks 属性可访问 SPContentType 对象中的列引用的集合。此属性返回一个 SPFieldLinkCollection 对象。

    备注

    SPContentType 对象还具有一个返回 SPFieldCollection 对象的 Fields 属性。您无法向此集合直接添加列。在将 SPFieldLink 对象添加到 FieldLinks 集合时,相应的 SPField 对象会自动添加到 Fields 集合。此集合中的每个 SPField 对象表示一个基列定义和列引用中指定的任何被覆盖属性的"合并视图"。

  5. 通过将 SPFieldLink 对象传递给 SPFieldLinkCollection 对象的 Add 方法,向内容类型添加列引用。

示例

以下示例说明派生自 SPFeatureReceiver 类的某个类的 FeatureActivated 方法。在激活功能时,FeatureActivated 方法中的代码会创建三个网站栏,并将它们添加到当前网站上的网站栏集合中。然后,该代码创建第一个内容类型 Financial Document,并将其添加到当前网站上的内容类型集合中。Financial Document 内容类型引用两个新的网站栏,即 DateOpened 和 Amount。紧接着,该代码创建第二个内容类型 Purchase Order,该内容类型继承自 Financial Document。Purchase Order 内容类型引用继承的网站栏 Title,以便在内容类型中更改其部分属性。此外,Purchase Order 内容类型包含对一个新网站栏 CostCenter 的引用。

该示例是在功能接收器作为网站集级别范围内的功能的一部分的情况下编写的。这意味着,Feature 属性返回一个 SPFeature 对象,该对象具有一个包含框式 SPSite 对象的 Parent 属性。您不应在 FeatureActivated 方法中释放此对象。但必须正常释放您的代码所创建的任何 SPWeb 对象,如该示例中所示。

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

    /* CREATE SITE COLUMNS */

    string columnGroup = "Financial Columns";

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

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

    // Cost Center Name
    string costCenterFieldName = site.Fields.Add("Cost Center", SPFieldType.Choice, false);
    SPFieldChoice costCenterField = (SPFieldChoice)site.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 = 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 = 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 Purchase Order content type.
    SPContentType purchaseOrderCType = new SPContentType(financialDocumentCType, site.ContentTypes, "Purchase Order");
    site.ContentTypes.Add(purchaseOrderCType);
    purchaseOrderCType = site.ContentTypes[purchaseOrderCType.Id];
    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);

    // Commit changes.
    purchaseOrderCType.Update();

    site.Dispose();
}
Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties)
    Dim siteCollection As SPSite = DirectCast(properties.Feature.Parent, SPSite)
    Dim site As SPWeb = siteCollection.RootWeb
    
    ' CREATE SITE COLUMNS 

    Dim columnGroup As String = "Financial Columns"
    
    ' Amount
    Dim amountFieldName As String = site.Fields.Add("Amount", SPFieldType.Currency, False)
    Dim amountField As SPFieldCurrency = DirectCast(site.Fields.GetFieldByInternalName(amountFieldName), SPFieldCurrency)
    amountField.Group = columnGroup
    amountField.DisplayFormat = SPNumberFormatTypes.TwoDecimals
    amountField.MinimumValue = 0
    amountField.Update()
    
    ' Date Opened
    Dim dateOpenedFieldName As String = site.Fields.Add("Date Opened", SPFieldType.DateTime, False)
    Dim dateOpenedField As SPFieldDateTime = DirectCast(site.Fields.GetFieldByInternalName(dateOpenedFieldName), SPFieldDateTime)
    dateOpenedField.Group = columnGroup
    dateOpenedField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly
    dateOpenedField.DefaultValue = "[today]"
    dateOpenedField.Update()
    
    ' Cost Center Name
    Dim costCenterFieldName As String = site.Fields.Add("Cost Center", SPFieldType.Choice, False)
    Dim costCenterField As SPFieldChoice = DirectCast(site.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 = 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 = 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 Purchase Order content type.
    Dim purchaseOrderCType As New SPContentType(financialDocumentCType, site.ContentTypes, "Purchase Order")
    site.ContentTypes.Add(purchaseOrderCType)
    purchaseOrderCType = site.ContentTypes(purchaseOrderCType.Id)
    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)
    
    ' Commit changes.
    purchaseOrderCType.Update()
    
    site.Dispose()
End Sub

请参阅

任务

如何:向网站中添加栏

引用

FieldRef 元素 (ContentType)

概念

字段和字段引用

列介绍