---
title: "カスタムファセットを作成する"
slug: "create-a-custom-facet"
abstract: "カスタムファセットモデルを作成し、同じキーでコンタクトファセットを定義する方法。"
---
# カスタムファセットを作成する

> !注Sitecore XP 10.2以降を使用している場合は、[Codeless Schema Extensionsモジュール](https://doc.sitecore.com/xp/en/users/104/sitecore-experience-platform/en/create-personalized-experiences-without-needing-help-from-a-developer.html)を使ってファセットを作成することもできます。

このトピックでは、FrequentFlyerInfoというファセットモデルを作成し、同じキーでコンタクトファセットを定義する方法を説明します。

> !重要ファセットキーは大文字で区別されます。

## ファセットモデルクラスを作成する

1.  継承するクラスを作成する Sitecore.XConnect.Facet
    
2.  xConnectの型制限に従うプロパティを1つ以上追加します。
    
    > !重要トラッカーの文脈でファセットを使う場合は、Serializableとマークしなければなりません。
    
    namespace Documentation
    {
     Serializable
     public class FrequentFlyerInfo : Sitecore.XConnect.Facet
     {
     public FrequentFlyerStatus Status { get; set; }
     public string FrequentFlyerID { get; set; } // Example: ABC12345
    
     public enum FrequentFlyerStatus
     {
     Unknown = 0,
     Bronze,
     Silver,
     Gold
     }
     }
    }
    
3.  必要に応じて、必須プロパティを示すコンストラクタを用いてください。以下の例では、FrequentFlyerIDが必須であり、設定されると更新できないプライベートセッターを持っています。
    
    > !重要すべてのファセットは空のコンストラクタを宣言 **しなければなりません** 。これはoDataの制限です。コンストラクタはプライベートにすることができます。
    
    namespace Documentation
    {
     Serializable
     public class FrequentFlyerInfo : Sitecore.XConnect.Facet
     {
     public FrequentFlyerInfo(string frequentFlyerId)
     {
     FrequentFlyerID = frequentFlyerId;
     }
    
     private FrequentFlyerInfo() { }
    
     public FrequentFlyerStatus Status { get; set; }
     public string FrequentFlyerID { get; private set; } // Example: ABC12345
    
     public enum FrequentFlyerStatus
     {
     Unknown = 0,
     Bronze,
     Silver,
     Gold
     }
     }
    }
    
4.  必要に応じて、ファセットやファセットプロパティにPIISensitiveまたはDoNotIndex属性を付けて装飾します。例えば、連絡先のパスポート番号はPIIデータです:
    
    namespace Documentation
    {
     Serializable
     public class FrequentFlyerInfo : Sitecore.XConnect.Facet
     {
     public FrequentFlyerInfo(string frequentFlyerId)
     {
     FrequentFlyerID = frequentFlyerId;
     }
    
     private FrequentFlyerInfo() { }
    
     public FrequentFlyerStatus Status { get; set; }
     public string FrequentFlyerID { get; private set; } // Example: ABC12345
    
     PIISensitive
     public string PassportNumber { get; set; }
    
     public enum FrequentFlyerStatus
     {
     Unknown = 0,
     Bronze,
     Silver,
     Gold
     }
     }
    }
    
5.  オプションで、FacetKey属性を使ってデフォルトのファセットキーを定義することもできます。デフォルトのファセットキーはxConnect Client APIの使い方を簡素化します。
    
    using Sitecore.XConnect;
    
    namespace Documentation
    {
     FacetKey(DefaultFacetKey)
     public class FrequentFlyerInfo : Sitecore.XConnect.Facet
     {
     public const string DefaultFacetKey = "FrequentFlyerInfo";
    
     public FrequentFlyerInfo(string frequentFlyerId)
     {
     FrequentFlyerID = frequentFlyerId;
     }
    
     public FrequentFlyerStatus Status { get; set; }
     public string FrequentFlyerID { get; set; } // Example: ABC12345
    
     public enum FrequentFlyerStatus
     {
     Unknown = 0,
     Bronze,
     Silver,
     Gold
     }
     }
    }
    

> !注Sitecoreのデフォルトのファセットキーは、コアコレクションモデルと並んで「 Sitecore.XConnect.Collection.Model.CollectionModel.FacetKeys」という静的クラスに存在しています。

## モデル内でカスタムファセットを定義します

.DefineFacet()メソッドを使ってコレクションモデルの新しいファセットを定義します。以下の例では、ファセットがSampleModelという名前のモデルに追加されています。Sitecoreコンテキストでモデルを使用する場合、モデルクラスは[クライアントの設定ファイルに追加](https://doc.sitecore.com/xp/ja/developers/91/sitecore-experience-platform/create-a-custom-model.html "Create a custom model")する必要があります。

using Sitecore.XConnect;
using Sitecore.XConnect.Schema;

namespace Documentation
{
 public class SampleModel
 {
 public static XdbModel Model { get; } = SampleModel.BuildModel();

 private static XdbModel BuildModel()
 {
 XdbModelBuilder modelBuilder = new XdbModelBuilder("SampleModel", new XdbModelVersion(0, 1));

 modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
 modelBuilder.DefineFacet<Contact, FrequentFlyerInfo>(FrequentFlyerInfo.DefaultFacetKey);

 return modelBuilder.BuildModel();
 }
 }
}

> !警告ファセットを登録しなければ、使用できません。「このインスタンスの型はスキーマ内のどの型とも対応しません」といったエラーが発生します。

## カスタムファセットを使う

xConnect Client APIを使ってファセットを扱うことができます。

-   [連絡先の側面を入手してください](https://doc.sitecore.com/xp/ja/developers/91/sitecore-experience-platform/get-contact-with-facets.html "Get contact with facets")
-   [接触面をセット](https://doc.sitecore.com/xp/ja/developers/91/sitecore-experience-platform/set-contact-facets.html "Set contact facets")
-   [インタラクションの側面を得てください](https://doc.sitecore.com/xp/ja/developers/91/sitecore-experience-platform/get-interaction-facets.html "Get interaction facets")
-   [集合の相互作用面](https://doc.sitecore.com/xp/ja/developers/91/sitecore-experience-platform/set-interaction-facets.html "Set interaction facets")

Sitecoreは複数の [内蔵機能を](https://doc.sitecore.com/xp/ja/developers/91/sitecore-experience-platform/use-core-collection-model-facets.html "Use core collection model facets")備えています。