コンテンツ タイプ内の列を参照する

最終更新日: 2015年1月13日

適用対象: SharePoint Foundation 2010

列はコンテンツ タイプに直接定義されません。列は、他の場所に定義され、コンテンツ タイプ内で参照されます。コンテンツ タイプ内で列を参照する理由として、次の 2 つがあります。

  • コンテンツ タイプに列を追加する。

  • 親のコンテンツ タイプから継承された列の特性を変更する。

    継承の詳細については、コンテンツ タイプ定義スキーマの ContentType 要素の Inherits 属性に関する説明を参照してください。

宣言型 XML では、FieldRef 要素を使用して列を参照します。コードでは、SPFieldLink オブジェクトを作成して、同じことを行います。

注意

Microsoft SharePoint Foundation では、フィールドは別の名前で列と呼ばれます。フィールドがユーザー インターフェイスでどのように表されるかについて説明する場合に、"列" という語がよく使用されます。

列と列の参照との違いについては、「フィールドとフィールド参照」を参照してください。

宣言型 XML を使用して列を参照する

コンテンツ タイプを作成する 1 つの方法は、機能の要素マニフェスト ファイルで、宣言型 XML を使用してコンテンツ タイプを宣言することです。機能をアクティブ化すると、コンテンツ タイプが作成されます。詳細については、「コンテンツ タイプ定義」を参照してください。

コンテンツ タイプ定義に列を含めるには、列を FieldRef 要素で参照します。参照される列には、サイトの列として既に存在する列、またはコンテンツ タイプを作成する機能と同じ機能で作成される新しい列を使用できます。また、FieldRef 要素を使用して、コンテンツ タイプがその親のコンテンツ タイプから継承した列を参照することもできます。この場合、列を参照する理由は、列を追加することではなく、コンテンツ タイプ内で列を使用するときにその一部の特性を変更することです。

宣言型 XML を使用して列参照を作成する

  1. コンテンツ タイプ定義で、FieldRef 要素を FieldRefs ノード内に追加します。

  2. FieldRef 要素の ID 属性の値を列の ID に設定して、列を参照します。

    値は、次の例のように、波かっこで囲まれた GUID の文字列表現として指定する必要があります。

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

    ID 属性は大文字と小文字が区別されます。したがって、属性は、Field 要素の ID 属性とまったく同じ値に設定する必要があります。

    組み込み列の ID については、%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES\fields にある fieldswss.xml ファイルを参照してください。

  3. FieldRef 要素の Name 属性の値を、列を表す Field 要素の Name 属性と同じ値に設定します。

  4. 必要に応じて、FieldRef 要素のその他の属性を設定して列を定義します。

    たとえば、列に対して表示されるテキストを変更するには、DisplayName 属性の値を設定します。

FieldRef 要素の多くの属性は、参照される Field 要素の属性と同じ名前と目的を持ちます。ID 属性と Name 属性のみは、同じ値にする必要があります。その他の属性については、FieldRef 要素で異なる値を使用することで、参照される列自体を変更せずに列をコンテンツ タイプで使用する場合、列の特性を変更できます。

重要重要

FieldRef 要素が、同じ要素マニフェストに定義されたフィールドを参照する場合でも、FieldRef 要素の ID、Name、DisplayName、および Required 属性は常に必要です。

以下の例は、3 つのサイト列と 2 つのサイト コンテンツ タイプを作成する機能の要素マニフェストを示しています。1 番目のコンテンツ タイプである Financial Document は、組み込みの Document コンテンツ タイプの子です。

Financial Document の定義では、DateOpened と Amount の 2 つの新しいサイト列が参照されます。DateOpened 列を参照する FieldRef 要素では、サイト列に定義されたとおりに、"Date Opened" ではなく "Date" として列名がレンダリングされるように DisplayName 属性が設定されます。

Purchase Order の定義では、3 番目の新しいサイト列である CostCenter が参照されます。また、サイト列に定義されたとおりに、"Cost Center" ではなく "Department" として列名がレンダリングされるように DisplayName 属性が設定されます。

Purchase Order は、その親である Financial Document から列参照を継承します。したがって、列を再度参照する必要はありません。コンテンツ タイプも、Title 列への参照を Financial Document から継承します。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 オブジェクトへの参照を取得します。

    SPField オブジェクトは、サイトを表す SPWeb オブジェクトの Fields プロパティに保持されているコレクションから取得できます。

  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 メソッド内のコードは、3 つのサイト列を作成し、それらを現在のサイトのサイト列コレクションに追加します。次に、コードはコンテンツ タイプ Financial Document を作成し、それを現在のサイトのコンテンツ タイプ コレクションに追加します。Financial Document コンテンツ タイプは、DateOpened と Amount の 2 つの新しいサイト列を参照します。次に、コードは、2 番目のコンテンツ タイプである Purchase Order を作成します。これは、Financial Document から継承します。Purchase Order コンテンツ タイプは、そのコンテンツ タイプ内で、継承されたサイト列 Title の一部のプロパティを変更するために、その列を参照します。また、Purchase Order コンテンツ タイプには、新しいサイト列の 1 つである CostCenter への参照が含まれます。

この例では、サイト コレクション レベルを範囲とする機能の一部であるように機能レシーバーが記述されています。つまり、Feature プロパティは、ボックス化された SPSite オブジェクトを含む Parent プロパティを持つ SPFeature オブジェクトを返します。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)

概念

フィールドとフィールド参照

列について