Expand related contact

When you retrieve an interaction or set of interactions, you can use RelatedContactExpandOptions to return the associated contact and contact facets:

var interactionTask = client.GetAsync<Interaction>(references, new InteractionExecutionOptions(new InteractionExpandOptions(WebVisit.DefaultFacetKey)
                    {
                        
                        Contact = new RelatedContactExpandOptions(PersonalInformation.DefaultFacetKey) // Pass in all contact facets that you want to return
                    }));

To access the contact and its facets when the interaction is returned, you must cast interaction.Contact (which is of type IEntityReference<Contact>) to a Contact. In the following example, a number of interactions are retrieved by ID and their associated contacts are expanded with the PersonalInformation facet:

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

namespace Documentation
{
    public class GetInteractionWithContact
    {
        // Async Example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    var interactionTask = client.GetAsync<Sitecore.XConnect.Interaction>(interactionRef, new InteractionExecutionOptions(new Sitecore.XConnect.InteractionExpandOptions(WebVisit.DefaultFacetKey)
                        {
                        Contact = new RelatedContactExpandOptions(PersonalInformation.DefaultFacetKey)
                        }
                        ));

                    var interaction = await interactionTask;

                    var webVisitFacet = interaction.GetFacet<WebVisit>();
                    var contact = interaction.Contact as Contact; // You must cast the contact reference to a contact entity
                    var contactFacet = contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey); // May be null if contact does not have a PersonalInformation facet set
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }

        public void ExamplSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("{B9814105-1F45-E611-82E6-34E6D7117DCB}"));
                    InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    var interaction = client.Get<Sitecore.XConnect.Interaction>(interactionRef, new InteractionExecutionOptions(new Sitecore.XConnect.InteractionExpandOptions(WebVisit.DefaultFacetKey)
                    {
                        Contact = new RelatedContactExpandOptions(PersonalInformation.DefaultFacetKey)
                    }));


                    var webVisitFacet = interaction.GetFacet<WebVisit>();
                    var contact = interaction.Contact as Contact; // You must cast the contact reference to a contact entity
                    var contactFacet = contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey); // May be null if contact does not have a PersonalInformation facet set
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }
    }
}