Set interaction facets

Current version: 9.2

Use the client.SetFacet() extension method to set interaction facets. You cannot update an interaction’s facets.

When you use a default facet value, the value isn't stored in the shard databases (xDB). Instead, the default value is populated when the facet is retrieved using xConnect. Examples of default values include false for a Boolean field, and 0 for an int32 type field.

The following example demonstrates how to set the LocaleInfo facet on a new interaction:

RequestResponse
using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;
using System.Collections.ObjectModel;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class AddInteractionFacetData
    {
        // Async Example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                {
                    try
                    {
                        var existingContactTask = client.GetAsync<Sitecore.XConnect.Contact>(new IdentifiedContactReference("twitter", "myrtlesitecore"), new Sitecore.XConnect.ContactExpandOptions());

                        var existingContact = await existingContactTask;

                        if (existingContact != null)
                        {
                            var channelId = Guid.Parse("5746c4f3-7e16-40d9-ba1d-14c70875724c");  // Replace with real channel ID
                            var userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1";

                            Interaction interaction = new Interaction(existingContact, InteractionInitiator.Brand, channelId, userAgent);
                            LocaleInfo localeInfo = new LocaleInfo() { GeoCoordinate = new GeoCoordinate(55.676097, 12.568337) };

                            client.SetFacet<LocaleInfo>(interaction, LocaleInfo.DefaultFacetKey, localeInfo);

                            client.AddInteraction(interaction);

                            await client.SubmitAsync();
                        }
                    }
                    catch (XdbExecutionException ex)
                    {
                        // Handle exception
                    }
                }
            }
        }

        // Sync example
        public async void ExampleSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                {
                    try
                    {
                        var existingContact = client.Get<Contact>(new IdentifiedContactReference("twitter", "myrtlesitecore"), new Sitecore.XConnect.ContactExpandOptions());

                        if (existingContact != null)
                        {
                            var channelId = Guid.Parse("5746c4f3-7e16-40d9-ba1d-14c70875724c");  // Replace with real channel ID
                            var userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1";

                            Interaction interaction = new Interaction(existingContact, InteractionInitiator.Brand, channelId, userAgent);
                            LocaleInfo localeInfo = new LocaleInfo() { GeoCoordinate = new GeoCoordinate(55.676097, 12.568337) };

                            client.SetFacet<LocaleInfo>(interaction, LocaleInfo.DefaultFacetKey, localeInfo);

                            client.AddInteraction(interaction);

                            client.Submit();
                        }
                    }
                    catch (XdbExecutionException ex)
                    {
                        // Handle exception
                    }
                }
            }
        }
    }
}

Do you have some feedback for us?

If you have suggestions for improving this article,