コンテンツ タイプの作成

最終更新日: 2010年11月1日

適用対象: SharePoint Foundation 2010

この記事の内容
コンテンツ タイプの継承
コンテンツ タイプの設計
宣言型 XML によるコンテンツ タイプの作成
コードを使用したコンテンツ タイプの作成

SharePoint Online で使用可能

開発者は、宣言型 XML またはおそらくサーバー コードを書いてコンテンツ タイプを作成します。開発者以外のユーザーによってインストールされ、さらに別のユーザーによってアクティブ化される機能の作成において、開発者は、通常、この作業を行います。したがって、開発者がコンテンツ タイプに対して実際に行う作業は、コンテンツ タイプを作成することではなく、コンテンツ タイプを定義することです。

最終的に、フィーチャーはインストールされ、ユーザーがそれをアクティブ化します。Microsoft SharePoint Foundation は、コードを実行するか宣言型 XML を読み取って、コンテンツ タイプを使用できるようにします。リストの管理権限を持つ管理ユーザーは、新しいコンテンツ タイプをリストまたはドキュメント ライブラリに適用します。これによりコンテンツ タイプは機能します。リストへのアイテムの追加が許可されているあらゆるユーザーがコンテンツ タイプを使用してコンテンツを作成できます。管理ユーザーは、列の追加や削除、あるいはその他の属性の変更を行って、コンテンツ タイプを独自の環境に適合するように変更できます。これらのすべては、開発者の範囲外の作業です。

重要な点は、コンテンツ タイプを "作成" することは、実際には、コンテンツ タイプの定義を作成することを意味するということです。適切なコンテンツ タイプの定義では、コンテンツ タイプが最終的にどのように使用されるかが予測されています。

コンテンツ タイプの継承

ゼロからコンテンツ タイプを作成するということはありません。代わりに、基礎となる既存のコンテンツ タイプを選択し、新しいコンテンツ タイプを作成します。Microsoft SharePoint Foundation を使用して組み込みのコンテンツ タイプに対して行う処理は、まさにこの作業です。SharePoint Foundation で提供されるコンテンツ タイプの階層については、「コンテンツ タイプの基本的な階層」を参照してください。

新しいコンテンツ タイプの作成先の Web サイトは、派生元のコンテンツ タイプの範囲内にある必要があります。したがって、コンテンツ タイプの定義を作成する場合は、このことを考慮する必要があります。詳細については、「コンテンツ タイプの範囲」を参照してください。

SharePoint Foundation がサポートするコンテンツ タイプの継承形式には制限があります。新しいコンテンツ タイプを定義する機能をアクティブ化すると、親コンテンツ タイプに定義された列とその他のメタデータが子コンテンツ タイプに渡されます。自動で継承されるのはこの部分のみです。

注意

SharePoint Foundation の以前のバージョンでは、コンテンツ タイプが準備された後にコンテンツ タイプに追加された列とメタデータは、子のコンテンツ タイプに継承されませんでした。SharePoint Foundation 2010 では、この動作が変更されました。現在は、ユーザーが親に追加した列とメタデータを含めることができます。詳細については、ContentType 要素の Inherits 属性の説明を参照してください。

コンテンツ タイプが使用可能になり、それが使用されている間に、親のコンテンツ タイプの定義を変更できます。ただし、その変更は、派生したコンテンツ タイプに自動でプッシュ ダウンされません。継承のこの側面は、ユーザー インターフェイスまたはカスタム コードを使用して手動で開始する必要があります。詳細については、「子コンテンツ タイプを更新する」を参照してください。

ただし、コンテンツ タイプの継承により、派生クラスと同じようにコンテンツ タイプを操作できます。たとえば、基本クラスのプロパティをオーバーライドできるように、親のコンテンツから派生したコンテンツ タイプの属性を変更できます。列の追加、変更、削除を行ったり、さまざま新規作成フォーム、編集フォーム、表示フォームを指定したり、さまざまなドキュメント テンプレートを指定したりできます。つまり、アプリケーション フレームワークの開発で基本クラスを作成するように、基本コンテンツ タイプを作成できます。たとえば、組織内のすべての財務ドキュメントに共通した特性を持つ Financial Document という名前のコンテンツ タイプを定義できます。次に、この Financial Document を、Invoice、Purchase Order のような、新しいコンテンツ タイプの基礎として使用できます。新しいコンテンツ タイプは、列とその他のメタデータを Financial Document から継承します。したがって、共通の要素を親のコンテンツ タイプに 1 回定義するだけで済みます。

独自のコンテンツ タイプ階層を構築する方法を理解するには、SharePoint Foundation で、継承された基本コンテンツ タイプの属性がどのように使用され、どのように変更されるかを学習します。開始点として、ユーザー インターフェイス内のコンテンツ ギャラリーから始めることをお勧めします。[サイトの操作] メニューで [サイトの設定] を選択します。[サイトの設定] ページの [ギャラリー] で、[サイト コンテンツ タイプ] をクリックします。[サイト コンテンツ タイプ] ページで、必要なコンテンツ タイプのリンクをクリックします。

もう 1 つの適切な学習リソースは、%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES\ctype フォルダーにある ctypewss.xml ファイルです。このファイルには、多くの組み込みコンテンツ タイプの定義が含まれます。定義の詳細については、「コンテンツ タイプ定義」を参照してください。

コンテンツ タイプの設計

コンテンツ タイプ計画の包括的なガイドについては、「コンテンツ タイプとワークフローを計画する (SharePoint Server 2010)」を参照してください。以下に、いくつかの一般的な考慮事項を示します。

  • 範囲。コンテンツ タイプをサイト階層のどの場所で使用できるようにしますか。

    コンテンツ タイプをサイト コレクション全体で使用できるようにする必要がある場合、サイト コレクションを範囲とする機能にコンテンツ タイプを展開し、コンテンツ タイプをルート Web サイトのコンテンツ タイプ コレクションに追加するフィーチャー レシーバー クラスの FeatureActivated メソッドにコードを書きます。

  • 。範囲内にある既存のどのコンテンツ タイプを、コンテンツ タイプの派生元として選択しますか。

    コンテンツ タイプの作成先の Web サイトで親のコンテンツ タイプを使用できない場合、コンテンツ タイプは適切にアクティブ化されません。

  • 。既存のどのサイト列を使用できるようにしますか。

    親のコンテンツ タイプから継承されない列は、追加する必要があります。存在しない列は、作成する必要があります。

  • その他のリソース。コンテンツ タイプでは言語リソースが必要ですか。ドキュメント テンプレート、フォームが必要ですか。

    コンテンツ タイプでこれらのリソースのいずれかが必要な場合、それらのリソースの準備方法と、それらのリソースへのコンテンツ タイプのアクセス方法を把握する必要があります。

宣言型 XML によるコンテンツ タイプの作成

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

複数のコンテンツ タイプを同じ要素マニフェスト ファイルに定義できます。定義するコンテンツ タイプごとに ContentType 要素を含めます。ID 属性は必須であり、指定する値は、非常に特殊な形式に従う必要があります。正しい形式で指定されたコンテンツ タイプ ID は、新しいコンテンツ タイプだけでなく、その親のコンテンツ タイプも識別します。詳細については、「コンテンツ タイプ ID」を参照してください。

組み込みコンテンツ タイプから派生したコンテンツ タイプの ID を作成するには、組み込みコンテンツ タイプを識別する 16 進数文字列を先頭に指定し、2 つのゼロを付加した後に、すべての句読点を取り除いた GUID を追加します。たとえば、Item コンテンツ タイプを識別する 16 進数文字列は "0x01" です。Item から派生したコンテンツ タイプの ID は次のようになります。

0x0100A33D9AD9805788419BDAAC2CCB37509F

作成したコンテンツ タイプから継承される追加のコンテンツ タイプの ID を作成するには、"01"、"02", ... を追加します。

ContentType 要素のもう 1 つの必須属性は、表示名を指定する Name です。値には、リソースへの参照を $Resources:String 形式で指定できます。詳細については、「SharePoint ソリューションのローカライズ」を参照してください。名前は 128 文字を超えることはできません。また、名前に \ / : * ? " # % < > { } | ~ & の各文字、2 つの連続するピリオド (..)、特殊文字 (たとえばタブ) を含めることはできません。

コンテンツ タイプに列を含める場合、その列はコンテンツ タイプの定義内で作成しません。代わりに、他の場所で作成された列を参照します。列参照を含めるには、FieldRef 要素を ContentType 要素の子として追加します。参照される列には、サイトの列として既に存在する列、またはコンテンツ タイプを作成する機能と同じ機能で作成される新しい列を使用できます。サイト列の作成方法については、「フィールド定義」を参照してください。

また、FieldRef 要素を使用して、コンテンツ タイプがその親のコンテンツ タイプから継承した列を参照することもできます。この場合、列を参照する理由は、列を追加することではなく、コンテンツ タイプ内で列を使用するときにその一部の特性を変更することです。

FieldRef 要素の ID 属性は、参照する列を識別します。値は GUID の文字列表現で、波かっこが含まれます。値は大文字と小文字が区別されます。

ヒントヒント

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

コンテンツ タイプで新しいコンテンツ用のドキュメント テンプレートを使用する場合、そのドキュメント テンプレートは、ContentType 要素内の DocumentTemplate 要素で識別できます。ファイルは別に準備する必要があります。これを行う 1 つの方法は、Module 要素をマニフェストに追加し、そのファイルを機能と共に含めます。詳細については、「[方法] ファイルを準備する」を参照してください。

コンテンツ タイプの定義には、XMLDocuments 要素を使用してユーザー設定フォームの定義を含めることもできます。詳細については、「コンテンツ タイプのユーザー設定情報」を参照してください。

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

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

Invoice は、Financial Document から派生する 2 つの新しいコンテンツ タイプのうちの最初のコンテンツ タイプです。このコンテンツ タイプの定義では、3 番目の新しいサイト列である Client が参照されており、Required 属性が TRUE に設定されています。これにより、新しいアイテムを作成するときにこのフィールドのエントリが必要になります。Invoice タイプには、Title 列の FieldRef 要素が含まれます。この要素は継承されますが、DisplayName 属性の値を "Title" から "Service" に変更する目的で、この要素が含まれています。

Financial Document から派生する 2 番目のコンテンツ タイプは Purchase Order です。このコンテンツ タイプの定義では、4 番目の新しいサイト列である 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/">

  <!-- 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 全体の機能にアクセスでき、実行時にデバッグを行うこともできます。この 2 つの手法は、Microsoft Visual Studio 2010 の SharePoint 開発者ツールで提供されるテンプレートでサポートされます。

コンテンツ タイプを定義するコードを、SPFeatureReceiver クラスのサブクラスの FeatureActivated メソッドに挿入します。

SPContentType クラスのコンストラクターを呼び出してコンテンツ タイプを作成します。コンストラクターを呼び出す前に、以下の 3 つのアイテムを収集して引数として渡す必要があります。

  • 親のコンテンツ タイプを表す SPContentType オブジェクト。

    このオブジェクトは、コンテンツ タイプの作成先の Web サイトを表す SPWeb オブジェクトの AvailableContentTypes プロパティに返されるコレクションから取得できます。

  • コンテンツ タイプの作成先の Web サイトのコレクションを表す SPContentTypeCollection オブジェクト。

    このオブジェクトを取得するには、コンテンツ タイプの作成先の Web サイトを表す SPWeb オブジェクトの ContentTypes プロパティにアクセスします。

  • コンテンツ タイプに指定する名前が含まれる文字列。

    指定する名前はコンテンツ タイプの表示名として使用されます。一部の文字はコンテンツ タイプの名前に使用できません。提案された名前は、静的メソッド ValidateName(String) を呼び出して検証できます。このメソッドにより、無効な文字があるかどうかが確認されますが、重複した名前があるかどうかは確認されません。

コンストラクターを呼び出して新しい SPContentType オブジェクトを作成した後に、Web サイトのコレクションを表す SPContentTypeCollection オブジェクトの Add(SPContentType) メソッドの別の呼び出しで、作成したオブジェクトを Web サイトのコンテンツ タイプ コレクションに追加します。この手順によって、コンテンツ タイプの初期化が完了します。

注意注意

同じ名前のコンテンツ タイプがコレクション内に既に存在する場合、Add(SPContentType) メソッドは、SPException 例外をスローします。

コンテンツ タイプのプロパティを設定する場合、Add メソッドで返される SPContentType オブジェクトを保存する必要があります。

コンテンツ タイプに列を追加するには、SPFieldLink オブジェクト内の各列を参照します。次に、コンテンツ タイプの FieldLinks プロパティによって返される SPFieldLinkCollection オブジェクトの Add(SPFieldLink) メソッドを呼び出します。

コンテンツ タイプの構成を完了すると、Update() メソッドを呼び出して、データベースに対して変更をコミットします。

次の例では、前述の例とまったく同じサイト列とコンテンツ タイプが作成されます。唯一の違いは、この例は、宣言型 XML を使用する代わりに、機能レシーバーの FeatureActivated メソッド内でコードを使用することです。

FeatureActivated メソッドの properties パラメーターは SPFeatureReceiverProperties オブジェクトです。このオブジェクトの Feature プロパティは、ボックス付きの SPSite オブジェクトまたはボックス付きの SPWeb オブジェクトを含む Parent プロパティを持つ SPFeature オブジェクトを返します。これらのオブジェクトを破棄しないでください。ただし、コードで作成した新しいすべての SPWeb または SPSite オブジェクトは破棄する必要があります。詳細については、「オブジェクトの破棄」を参照してください。

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 Server 2010)

Create Custom Content Types in SharePoint 2010

チュートリアル: カスタムのフィールド、コンテンツ タイプ、リスト定義、およびリスト インスタンスの作成 (英語)

How to: Programmatically Create Content Types (英語)