Add contacts

Abstract

How to add contacts with or without identifiers.

Use the client.AddContact() method to add a new contact. Contacts can be added with or without identifiers. An anonymous alias identifier is added to all new contacts, and contact IDs are sequentially generated by the xConnect service layer.

Add a single contact

In the folowing example, a single contact without identifiers or facets is created and submitted to xConnect:

using Sitecore.XConnect;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class AddContact
    {
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var firstContact = new Sitecore.XConnect.Contact();
                    client.AddContact(firstContact); // Extension found in Sitecore.XConnect.Operations

                    // Submits the batch, which contains two operations
                    await client.SubmitAsync();
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exception
                }
            }
        }

        public void Example()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var firstContact = new Sitecore.XConnect.Contact();
                    client.AddContact(firstContact); // Extension found in Sitecore.XConnect.Operations

                    // Submits the batch, which contains two operations
                    client.Submit();
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exception
                }
            }
        }
    }
}

Add multiple contacts

In the following example, two contacts without facets or identifiers are created and submitted as a single batch:

using Sitecore.XConnect;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class AddMultipleContact
    {
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var firstContact = new Sitecore.XConnect.Contact();
                    client.AddContact(firstContact); // Extension found in Sitecore.XConnect.Operations

                    var secondContact = new Sitecore.XConnect.Contact();
                    client.AddContact(secondContact); // Extension found in Sitecore.XConnect.Operations

                    // Submits the batch, which contains two operations
                    await client.SubmitAsync();
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exception
                }
            }
        }

        public void Example()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var firstContact = new Sitecore.XConnect.Contact();
                    client.AddContact(firstContact); // Extension found in Sitecore.XConnect.Operations

                    var secondContact = new Sitecore.XConnect.Contact();
                    client.AddContact(secondContact); // Extension found in Sitecore.XConnect.Operations

                    // Submits the batch, which contains two operations
                    client.Submit();
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exception
                }
            }
        }
    }
}

Large numbers of contacts should be submitted in smaller batches. Refer to the following documentation for more information about batching: