Sitecore Experience Platform

Access contact and interaction data from activity type

You can access the context Contact being processed and an Interaction, if the activity was invoked as a result of an interaction.

Important

The Interaction property will only be populated if the activity is being invoked as the result of an interaction. If an activity was invoked as a result of a contact facet being updated, the Interaction property will be null. Check for null before using.

The following example demonstrates how to access the contact, the current interaction (if available), facets, and events in the context of an activity:

using System;
using System.Linq;
using Sitecore.Xdb.MarketingAutomation.Model.Processing;
using Sitecore.Xdb.MarketingAutomation.Model.Results;
using Sitecore.Xdb.MarketingAutomation.Model.Rules;
using Sitecore.XConnect.Collection.Model;

namespace Documentation.Examples
{
    public class MidweekSalesReminder : IActivity
    {
        public IActivityServices Services { get; set; }

        public ActivityResult Invoke(IContactProcessingContext context)
        {
            if (context.Interaction != null)
            {
                // Get event of specific type from CURRENT interaction
                var purchaseEvent = context.Interaction.Events
                    .OrderByDescending(e => e.Timestamp)
                    .FirstOrDefault(evt => evt.DefinitionId == Guid.Parse("9FF6AB6D-AA45-4DDA-BFB3-674170BE2448"));

                                    // Get web facet
                                    var webFacet = context.Interaction.WebVisit();
            }

            // Get contact facet
            var personalFacet = context.Contact.Personal();

            return new SuccessStay();
        }
    }
}

The number of interactions, interaction facets, and contact facets that are available is controlled by the contact loader. The contact loader is configured in the xConnect installation folder, in the \App_data\jobs\continuous\AutomationEngine\App_Data\Config\sitecore\MarketingAutomation folder, in the sc.MarketingAutomation.ContactLoader.xml file.

By default, interactions from the last 30 days are returned:

<MarketingAutomation.Loading.FixedHistoryConfigurator>
    <Type>Sitecore.Xdb.MarketingAutomation.Loading.FixedHistoryConfigurator, Sitecore.Xdb.MarketingAutomation</Type>
    <As>Sitecore.Xdb.MarketingAutomation.Core.Loading.IContactExpandOptionsConfigurator, Sitecore.Xdb.MarketingAutomation.Core</As>
    <LifeTime>Singleton</LifeTime>
    <Options>
    <MaximumAge>30.00:00:00</MaximumAge>
    </Options>
</MarketingAutomation.Loading.FixedHistoryConfigurator>

You must specify which contact and interaction facets are available by adding the relevant facet keys to the IncludeFacetNames section:

<!-- Contact facets -->
<MarketingAutomation.Loading.ContactFacetsConfigurator>
    <Type>Sitecore.Xdb.MarketingAutomation.Loading.ContactFacetsConfigurator, Sitecore.Xdb.MarketingAutomation</Type>
    <As>Sitecore.Xdb.MarketingAutomation.Core.Loading.IContactExpandOptionsConfigurator, Sitecore.Xdb.MarketingAutomation.Core</As>
    <LifeTime>Singleton</LifeTime>
    <Options>
    <IncludeFacetNames>
        <Facet1>MyIncludeFacet</Facet1>
        <Facet2>MyIncludeFacet2</Facet2>
    </IncludeFacetNames>
    <ExcludeFacetNames>
        <Facet1>MyExcludeFacet</Facet1>
        <Facet2>MyExcludeFacet2</Facet2>
    </ExcludeFacetNames>
    </Options>
</MarketingAutomation.Loading.ContactFacetsConfigurator>

<!-- Interaction facets -->
 <MarketingAutomation.Loading.InteractionFacetsConfigurator>
    <Type>Sitecore.Xdb.MarketingAutomation.Loading.InteractionFacetsConfigurator, Sitecore.Xdb.MarketingAutomation</Type>
    <As>Sitecore.Xdb.MarketingAutomation.Core.Loading.IContactExpandOptionsConfigurator, Sitecore.Xdb.MarketingAutomation.Core</As>
    <LifeTime>Singleton</LifeTime>
    <Options>
    <IncludeFacetNames>
        <Facet1>MyIncludeFacet</Facet1>
        <Facet2>MyIncludeFacet2</Facet2>
    </IncludeFacetNames>
    <ExcludeFacetNames>
        <Facet1>MyExcludeFacet</Facet1>
        <Facet2>MyExcludeFacet2</Facet2>
    </ExcludeFacetNames>
    </Options>
</MarketingAutomation.Loading.InteractionFacetsConfigurator>

Note

The AutomationPlanEnrollmentCache and AutomationPlanExit facets are required by the engine and are always available.

It is inefficient to always load all facets for a contact or interaction, if those facets are only required by a single activity type which is not often used. Activity types implementations are granted access to an xConnect client through the Services.Collection property of the activity type. This client can be used to load additional data without changing configuration for the entire engine.

using System;
using System.Linq;
using Sitecore.Xdb.MarketingAutomation.Model.Processing;
using Sitecore.Xdb.MarketingAutomation.Model.Results;
using Sitecore.Xdb.MarketingAutomation.Model.Rules;
using Sitecore.XConnect.Collection.Model;

namespace Documentation.Examples
{
    public class CustomFacetEvaluator : IActivity
    {
        public IActivityServices Services { get; set; }

        public ActivityResult Invoke(IContactProcessingContext context)
                    {
                            var contact = context.Contact;

                            // check to see if the "custom1" facet was loaded already
                            if (!contact.ExpandOptions.FacetKeys.Contains("custom1"))
                            {
                                    var expandOptions = new ContactExpandOptions("custom1");
                                    contact = Services.Collection.GetContactAsync(contact, expandOptions).ConfigureAwait(false).GetAwaiter().GetResult();
                            }

                            var facet = contact.GetFacet<CustomFacet>("custom1");

            return new SuccessStay();
        }
    }
}