Get contact with interactions

Current version: 10.4

Use RelatedInteractionsExpandOptions to specify the range of interactions and interaction facets that should be returned with each contact. In the following example, interactions from the last 10 days are returned with each contact, limited to a maximum of 30 interactions. The LocaleInfo facet is also returned for each interaction if it is present.

RequestResponse
var contactsTask = client.GetAsync<Contact>(references, new ContactExecutionOptions(new ContactExpandOptions() {
    Interactions = new RelatedInteractionsExpandOptions(LocaleInfo.DefaultFacetKey)
    {
        EndDateTime = DateTime.UtcNow.AddDays(-10),
        Limit = 30
    }
}));

The following table describes how to use the RelatedInteractionsExpandOptions class:

Property

Description

FacetKeys

Read-only property - passed in to the constructor as an array of strings and specifies which facets to return with each interaction

StartDateTime

The interaction start date/time must be equal to or greater than this value. Use DateTime.MinValue to get the earliest interactions.

EndDateTime

The interaction end date/time must be equal to or less than this value. Use DateTime.MaxValue to get the most recent interactions.

Limit

The maximum number of interactions to return between the specified dates.

The following example demonstrates how to use ContactExpandOptions and RelatedInteractionsExpandOptions together. Contacts are returned with the PersonalInformation facet and any interaction older than ten days, limited to 30 interactions.

RequestResponse
using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect.Operations;
using System;
using System.Collections.Generic;
using Sitecore.XConnect;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class GetContactWithInteractions
    {
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var references = new List<Sitecore.XConnect.IEntityReference<Sitecore.XConnect.Contact>>()

                    {
                        // Contact ID
                        new Sitecore.XConnect.ContactReference(new Guid("{A2814105-1F45-E611-52E6-34E6D7117DCB}")),
                        // Username
                        new Sitecore.XConnect.IdentifiedContactReference("twitter", "myrtlesitecore"),
                        // Ad network ID
                        new Sitecore.XConnect.IdentifiedContactReference("adnetwork", "AB973934540244")
                    };

                    var contactsTask = client.GetAsync<Sitecore.XConnect.Contact>(references, new ContactExecutionOptions(new Sitecore.XConnect.ContactExpandOptions(PersonalInformation.DefaultFacetKey)
                    {
                        Interactions = new Sitecore.XConnect.RelatedInteractionsExpandOptions(LocaleInfo.DefaultFacetKey)
                        {
                            StartDateTime = DateTime.MinValue,
                            EndDateTime = DateTime.UtcNow.AddDays(-10),
                            Limit = 30
                        }
                    }));

                    var results = await contactsTask;

                    foreach (var result in results)
                    {
                        if (result.Exists)
                        {
                            // Do something
                            var contact = result.Entity;
                        }
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }

        public async void Example()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var references = new List<Sitecore.XConnect.IEntityReference<Sitecore.XConnect.Contact>>()

                    {
                        // Contact ID
                        new Sitecore.XConnect.ContactReference(new Guid("{A2814105-1F45-E611-52E6-34E6D7117DCB}")),
                        // Username
                        new Sitecore.XConnect.IdentifiedContactReference("twitter", "myrtlesitecore"),
                        // Ad network ID
                        new Sitecore.XConnect.IdentifiedContactReference("adnetwork", "AB973934540244")
                    };

                    var results = client.Get<Sitecore.XConnect.Contact>(references,  new ContactExecutionOptions(new Sitecore.XConnect.ContactExpandOptions(PersonalInformation.DefaultFacetKey)
                    {
                        Interactions = new Sitecore.XConnect.RelatedInteractionsExpandOptions(LocaleInfo.DefaultFacetKey)
                        {
                            StartDateTime = DateTime.MinValue,
                            EndDateTime = DateTime.UtcNow.AddDays(-10),
                            Limit = 30
                        }
                    }));

                    foreach (var result in results)
                    {
                        if (result.Exists)
                        {
                            // Do something
                            var contact = result.Entity;
                        }
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }
    }
}

Get all interactions

Set the following minimum and maximum values to return all available interactions with each contact.

RequestResponse
var contactsTask = client.GetAsync<Contact>(new Sitecore.XConnect.ContactReference(new Guid("{A2814105-1F45-E611-52E6-34E6D7117DCB}")), new ContactExecutionOptions(new ContactExpandOptions() {
    Interactions = new RelatedInteractionsExpandOptions()
    {
        StartDateTime = DateTime.MinValue,
        EndDateTime = DateTime.MaxValue,
        Limit = int.MaxValue
    }
}));

Filtering interactions

You may only want to work with a subset of a contact’s interactions, such as any interaction with the WebVisit facet. There are two ways to filter a contact’s interactions:

  • Return all available interactions using RelatedInteractionsExpandOptions and filter the collection in memory

  • Search all interactions and filter by contact ID

In the following example, a contact and all available interactions for that contact are returned using the .Get<Contact>/.GetAsync method. The collection of interactions is then filtered down to a subset of interactions that have the WebVisit facet and at least one Outcome:

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

namespace Documentation
{
    public class FilterInteractions
    {
        // Async example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var contactsTask = client.GetAsync<Contact>(new IdentifiedContactReference("twitter", "myrtlesitecore"), new ContactExecutionOptions(new ContactExpandOptions(PersonalInformation.DefaultFacetKey)
                    {
                        Interactions = new RelatedInteractionsExpandOptions(WebVisit.DefaultFacetKey)
                        {
                            StartDateTime = DateTime.MinValue,
                            Limit = int.MaxValue
                        }
                    }));

                    var contacts = await contactsTask;

                                        {
                    // All interactions
                    var interactions = contacts.Interactions;

                    // Interactions with WebVisit facet and at least one Outcome
                        var interactionsWithOutcomes = interactions.Where(x => x.WebVisit() != null && x.Events.OfType<Outcome>().Any());
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }

        // Sync example
        public void Example()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var contacts = client.Get<Contact>(new IdentifiedContactReference("twitter", "myrtlesitecore"), new ContactExecutionOptions(new Sitecore.XConnect.ContactExpandOptions(PersonalInformation.DefaultFacetKey)
                    {
                        Interactions = new RelatedInteractionsExpandOptions(WebVisit.DefaultFacetKey)
                        {
                            StartDateTime = DateTime.MinValue,
                            Limit = int.MaxValue
                        }
                    }));

                    
                    // All interactions
                        var interactions = contacts.Interactions;

                    // Interactions with WebVisit facet and outcome
                        var interactionsWithOutcomes = interactions.Where(x => x.WebVisit() != null && x.Events.OfType<Outcome>().Any());
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }
    }
}

In the following example, interaction search is used to retrieve all interactions that have at least one Outcome and match a particular contact ID:

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

namespace Documentation
{
    public class SearchByContactID
    {
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var contactID = Guid.Parse("ea991dce-1f9b-443c-84a5-f83b46cbc0f8");

                    // IMPORTANT: You cannot check facets for null; check a property of the facet instead, such as SiteName for WebVisit
                    var queryable = client.Interactions
                        .Where(x => x.Contact.Id == contactID
                        && x.Events.OfType<Outcome>().Any()
                        && x.WebVisit().SiteName != "")
                        .WithExpandOptions(new InteractionExpandOptions(WebVisit.DefaultFacetKey) { Contact = new RelatedContactExpandOptions(PersonalInformation.DefaultFacetKey) });

                    var enumerator = await queryable.GetBatchEnumerator(10);

                    while (await enumerator.MoveNext())
                    {
                        foreach (var item in enumerator.Current)
                        {
                            var listOfOutcomes = item.Events.OfType<Outcome>();
                            var webFacet = item.WebVisit();
                        }
                    }

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

        // Sync example
        public async void Example()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var contactID = Guid.Parse("ea991dce-1f9b-443c-84a5-f83b46cbc0f8");

                    // IMPORTANT: You cannot check facets for null; check a property of the facet instead, such as SiteName for WebVisit
                    var queryable = client.Interactions
                        .Where(x => x.Contact.Id == contactID
                        && x.Events.OfType<Outcome>().Any()
                        && x.WebVisit().SiteName != "")
                        .WithExpandOptions(new InteractionExpandOptions(WebVisit.DefaultFacetKey) { Contact = new RelatedContactExpandOptions(PersonalInformation.DefaultFacetKey) });

                    var enumerator = queryable.GetBatchEnumeratorSync(10);

                    while (enumerator.MoveNext())
                    {
                        foreach (var item in enumerator.Current)
                        {
                            var listOfOutcomes = item.Events.OfType<Outcome>();
                            var webFacet = item.WebVisit();
                        }
                    }

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

Do you have some feedback for us?

If you have suggestions for improving this article,