Expand related contact

Version: 9.1

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

RequestResponse
var interactionTask = client.GetAsync<Interaction>(references, new InteractionExpandOptions().Expand<WebVisit>()
{
    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:

RequestResponse
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 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 Sitecore.XConnect.InteractionExpandOptions()
                        {
                        Contact = new RelatedContactExpandOptions(PersonalInformation.DefaultFacetKey)
                        }
                        .Expand<WebVisit>());

                    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<Interaction>(interactionRef, new Sitecore.XConnect.InteractionExpandOptions()
                    {
                        Contact = new RelatedContactExpandOptions(PersonalInformation.DefaultFacetKey)
                    }
                    .Expand<WebVisit>());


                    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
                }
            }
        }
    }
}

Do you have some feedback for us?

If you have suggestions for improving this article,