1. 連絡先を操作する

コンタクトファセットの設定

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

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("[email protected]", 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("[email protected]", 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 ContactExecutionOptions(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 ContactExecutionOptions(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 ContactExecutionOptions(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("[email protected]", true);
                            facet.PreferredKey = "Work";

                            // Set the updated facet
                            client.SetFacet(existingContact, EmailAddressList.DefaultFacetKey, facet);
                        }
                        else
                        {
                            // Facet is new
                            EmailAddressList emails = new EmailAddressList(new EmailAddress("[email protected]", 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 ContactExecutionOptions(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("[email protected]", true);
                            facet.PreferredKey = "Work";

                            // Set the updated facet
                            client.SetFacet(existingContact, EmailAddressList.DefaultFacetKey, facet);
                        }
                        else
                        {
                            // Facet is new
                            EmailAddressList emails = new EmailAddressList(new EmailAddress("[email protected]", true), "Work");

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

                        client.Submit();
                    }

                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }
    }
}
この記事を改善するための提案がある場合は、 お知らせください!