---
title: "接触面をセット"
slug: "set-contact-facets"
abstract: "xConnect Client APIを使ってコンタクトの面値を設定する方法。"
---
# 接触面をセット

client.SetFacet()メソッドを使ってコンタクトファセットを作成または更新してください。

デフォルトのファセット値を使うと、その値はシャードデータベース(xDB)に保存されません。代わりに、xConnectでファセットを取得する際にデフォルト値が入力されます。デフォルト値の例としては、ブールフィールドのfalseやint32型フィールドの0があります。

既存の連絡先のファセットを更新する際には、以下の点を念頭に置いてください:

-   既存の契約の既存のファセットを更新する場合は、そのファセットを ContactExpandOptions
-   既存のコンタクトで初めてファセットを設定する場合、そのファセットをリクエストする必要はありません ContactExpandOptions
-   すでに設定されたコンタクトファセットを更新するには、既存のファセットオブジェクトを取得し、個別のプロパティを設定する必要があります。ファセット全体を上書きしたりnullに設定すると例外が発生します

> !重要ファセットはSubmitAsync() / Submit()成功直後にコンタクトのFacetsコレクションで利用可能です。

## 新しい連絡先に新しい面を設定する

以下の例では、新しい接触に3つのファセットを追加します。この例は、各client.SetFacet() オーバーロードの使い方を示しています。

using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Collection.Model;

namespace Documentation
{
 public class AddFacetData
 {
 // Async example
 public async void ExampleAsync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 {
 try
 {
 Contact contact = new Contact(new ContactIdentifier("twitter", "myrtlesitecore", ContactIdentifierType.Known));

 client.AddContact(contact);

 // Facet with a reference object, key is specified
 PersonalInformation personalInfoFacet = new PersonalInformation()
 {
 FirstName = "Myrtle",
 LastName = "McSitecore"
 };

 FacetReference reference = new FacetReference(contact, PersonalInformation.DefaultFacetKey);

 client.SetFacet(reference, personalInfoFacet);

 // Facet without a reference, using default key
 EmailAddressList emails = new EmailAddressList(new EmailAddress("myrtle@test.test", true), "Home");

 client.SetFacet(contact, emails);

 // Facet without a reference, key is specified

 AddressList addresses = new AddressList(new Address() { AddressLine1 = "Cool Street 12", City = "Sitecore City", PostalCode = "ABC 123" }, "Home");

 client.SetFacet(contact, AddressList.DefaultFacetKey, addresses);

 // Submit operations as batch
 await client.SubmitAsync();
 }
 catch (XdbExecutionException ex)
 {

 }
 }
 }
 }

 // Sync example
 public async void ExampleSync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 {
 try
 {
 Contact contact = new Contact(new ContactIdentifier("twitter", "myrtlesitecore", ContactIdentifierType.Known));

 client.AddContact(contact);

 // Facet with a reference object, key is specified
 PersonalInformation personalInfoFacet = new PersonalInformation()
 {
 FirstName = "Myrtle",
 LastName = "McSitecore"
 };

 FacetReference reference = new FacetReference(contact, PersonalInformation.DefaultFacetKey);

 client.SetFacet(reference, personalInfoFacet);

 // Facet without a reference, using default key
 EmailAddressList emails = new EmailAddressList(new EmailAddress("myrtle@test.test", true), "Home");

 client.SetFacet(contact, emails);

 // Facet without a reference, key is specified

 AddressList addresses = new AddressList(new Address() { AddressLine1 = "Cool Street 12", City = "Sitecore City", PostalCode = "ABC 123" }, "Home");

 client.SetFacet(contact, AddressList.DefaultFacetKey, addresses);

 // Submit operations as batch
 client.Submit();
 }
 catch (XdbExecutionException ex)
 {

 }
 }
 }
 }
 }
}
## 既存のコンタクトに新しいファセットを設定する

以下の例は、既存の接触にPersonalInformation面を設定する方法を示しています。この例では、接触にはまだPersonalInformation面がありません。

using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Collection.Model;
using System.Threading.Tasks;

namespace Documentation
{
 public class AddFacetDataToExistingContact
 {
 // Async example
 public async void ExampleAsync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 {
 try
 {
 Task<Contact> contactTask = client.GetAsync<Contact>(new IdentifiedContactReference("twitter", "myrtlesitecore"), new ContactExpandOptions(PersonalInformation.DefaultFacetKey));

 Contact contact = await contactTask;

 if (contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey) == null)
 {
 // Only create new facet if one does not already exist
 PersonalInformation personalInfoFacet = new PersonalInformation()
 {
 FirstName = "Myrtle",
 LastName = "McSitecore"
 };

 client.SetFacet(contact, PersonalInformation.DefaultFacetKey, personalInfoFacet);

 await client.SubmitAsync();
 }
 }
 catch (XdbExecutionException ex)
 {

 }
 }
 }
 }

 // Sync example
 public void ExampleSync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 {
 try
 {
 Contact contact = client.Get<Contact>(new IdentifiedContactReference("twitter", "myrtlesitecore"), new ContactExpandOptions(PersonalInformation.DefaultFacetKey));

 if (contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey) == null)
 {
 // Only create new facet if one does not already exist
 PersonalInformation personalInfoFacet = new PersonalInformation()
 {
 FirstName = "Myrtle",
 LastName = "McSitecore"
 };

 client.SetFacet(contact, PersonalInformation.DefaultFacetKey, personalInfoFacet);

 client.Submit();
 }
 }
 catch (XdbExecutionException ex)
 {

 }
 }
 }
 }
 }
}
## 既存の連絡先のファセットを更新

以下の例は、既存のコンタクトのファセットを更新する方法を示しています。

using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Sitecore.XConnect.Client;

namespace Documentation
{
 public class UpdateFacet
 {
 public async void ExampleAsync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 // Retrieve contact
 var existingContactTask = client.GetAsync<Sitecore.XConnect.Contact>(new IdentifiedContactReference("twitter", "myrtlesitecore"), new Sitecore.XConnect.ContactExpandOptions(EmailAddressList.DefaultFacetKey));

 Sitecore.XConnect.Contact existingContact = await existingContactTask;

 if (existingContact != null)
 {
 // Retrieve facet by name
 var facet = existingContact.GetFacet<EmailAddressList>(EmailAddressList.DefaultFacetKey);

 if (facet != null)
 {
 // Change facet properties
 facet.PreferredEmail = new EmailAddress("myrtle@test.test", true);
 facet.PreferredKey = "Work";

 // Set the updated facet
 client.SetFacet(existingContact, EmailAddressList.DefaultFacetKey, facet);
 }
 else
 {
 // Facet is new
 EmailAddressList emails = new EmailAddressList(new EmailAddress("myrtle@test.test", true), "Work");

 client.SetFacet<EmailAddressList>(existingContact, EmailAddressList.DefaultFacetKey, emails);
 }

 await client.SubmitAsync();
 }
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }

 public void ExampleSync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 // Retrieve contact
 Sitecore.XConnect.Contact existingContact = client.Get<Sitecore.XConnect.Contact>(new IdentifiedContactReference("twitter", "myrtlesitecore"), new Sitecore.XConnect.ContactExpandOptions(EmailAddressList.DefaultFacetKey));

 if (existingContact != null)
 {
 // Retrieve facet by name
 var facet = existingContact.GetFacet<EmailAddressList>(EmailAddressList.DefaultFacetKey);

 if (facet != null)
 {
 // Change facet properties
 facet.PreferredEmail = new EmailAddress("myrtle@test.test", true);
 facet.PreferredKey = "Work";

 // Set the updated facet
 client.SetFacet(existingContact, EmailAddressList.DefaultFacetKey, facet);
 }
 else
 {
 // Facet is new
 EmailAddressList emails = new EmailAddressList(new EmailAddress("myrtle@test.test", true), "Work");

 client.SetFacet<EmailAddressList>(existingContact, EmailAddressList.DefaultFacetKey, emails);
 }

 client.Submit();
 }

 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }
 }
}