Add identifiers

Current version: 9.2

A contact identifier uniquely identifies a contact to external systems and can be added to new or existing contacts. New identifiers are available in the contact’s Identifiers collection as soon as the batch has been successfully submitted.

Add identifiers to a new contact

In the following example, a known identifier and an anonymous identifier are added to a new contact. You can pass identifiers into the Contact constructor or use the .AddContactIdentifier() method.

RequestResponse
using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Sitecore.XConnect.Operations;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Documentation
{
    public class GetContactIdentifiers
    {
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var contact = new Sitecore.XConnect.Contact(
                        new Sitecore.XConnect.ContactIdentifier("twitter", "myrtlesitecore", Sitecore.XConnect.ContactIdentifierType.Known)
                        );
                     client.AddContact(contact);
                     client.AddContactIdentifier(contact, new Sitecore.XConnect.ContactIdentifier("ad-network", "ABC123456", Sitecore.XConnect.ContactIdentifierType.Anonymous));
                  
                     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 contact = new Sitecore.XConnect.Contact(
                        new Sitecore.XConnect.ContactIdentifier("twitter", "myrtlesitecore", Sitecore.XConnect.ContactIdentifierType.Known)
                        );
                     client.AddContact(contact);                    
                     client.AddContactIdentifier(contact, new Sitecore.XConnect.ContactIdentifier("ad-network", "ABC123456", Sitecore.XConnect.ContactIdentifierType.Anonymous));
              
                    client.Submit();
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exception
                }
            }
        }

    }
}
Note

You can use the .AddContactIdentifier method to add an identifier to a new contact.

Add identifiers to an existing contact

In the following example, an identifier is added to an existing contact using the .AddContactIdentifier() method.

RequestResponse
using System;
using Sitecore.XConnect;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class UpdateContact
    {
        // Async example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    // Retrieve contact from xConnect first
                    var existingContactTask = client.GetAsync<Sitecore.XConnect.Contact>(new Sitecore.XConnect.IdentifiedContactReference("twitter", "myrtlesitecore"), new Sitecore.XConnect.ContactExpandOptions("SitecoreSkyFrequentFlyerInfo"));

                    Sitecore.XConnect.Contact existingContact = await existingContactTask;

                    if (existingContact != null)
                    {
                        client.AddContactIdentifier(existingContact, new Sitecore.XConnect.ContactIdentifier("twitter", "myrtlesitecore", Sitecore.XConnect.ContactIdentifierType.Known));

                        await client.SubmitAsync();
                    }
                }
                catch (Exception ex)
                {
                    // Handle exceptions
                }

            }
        }

        // Sync
        public void Example()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    // Retrieve contact from xConnect first
                    Contact existingContact = client.Get<Sitecore.XConnect.Contact>(new Sitecore.XConnect.IdentifiedContactReference("twitter", "myrtlesitecore"), new Sitecore.XConnect.ContactExpandOptions("SitecoreSkyFrequentFlyerInfo"));

                    if (existingContact != null)
                    {
                        client.AddContactIdentifier(existingContact, new Sitecore.XConnect.ContactIdentifier("twitter", "myrtlesitecore", Sitecore.XConnect.ContactIdentifierType.Known));

                        client.Submit();
                    }
                }
                catch (Exception ex)
                {
                    // Handle exceptions
                }

            }
        }
    }
}

Updating contact identifiers

Existing contact identifiers cannot be updated. Identifiers can only be removed and added. In the following example, an existing identifier is being removed using the .RemoveContactIdentifier() extension:

RequestResponse
using Sitecore.XConnect;
using System;
using System.Threading.Tasks;
using System.Linq;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class RemoveContactIdentifier
    {
        // Async example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var reference = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));

                    Task<Contact> contactTask = client.GetAsync<Sitecore.XConnect.Contact>(reference, new Sitecore.XConnect.ContactExpandOptions() { });

                    Sitecore.XConnect.Contact contact = await contactTask;

                    var identifierToRemove = contact.Identifiers.Where(x => x.Identifier == "sitecoremyrtle").FirstOrDefault();

                    client.RemoveContactIdentifier(contact, identifierToRemove);

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

        // Sync example
        public void ExampleSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var reference = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));

                    Contact contact = client.Get<Sitecore.XConnect.Contact>(reference, new Sitecore.XConnect.ContactExpandOptions() { });

                    var identifierToRemove = contact.Identifiers.Where(x => x.Identifier == "sitecoremyrtle").FirstOrDefault();

                    client.RemoveContactIdentifier(contact, identifierToRemove);

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

Do you have some feedback for us?

If you have suggestions for improving this article,