0

I'm in a situation that an identifier from a 3rd party source can change frequently. I do want to save this id (but as an anonymous identifier) and update it when I notice a change.

The Sitecore documentation is pretty clear on how to add an Identifier.

But how do I update do an update?

  1. Do I just call client.AddContactIdentifier("mySource", ...)
  2. Do I remove the identifier first client.RemoveContactIdentifier("mySource", ...) and then call client.AddContactIdentifier("mySource", ...)
2

As mentioned in the Sitecore documentation, "Existing contact identifiers cannot be updated", which means that contact identifiers are immutable objects.

In your question, you have basically listed out the 2 possible option to take

  1. Add additional identifier
  2. Replace identifier (In reality, you would need to remove and then add the identifier since identifiers are immutable)

So, this is really a matter of what suits your use case better. If old identifier is no longer useful, then option 2 probably suit you better.

  • Do you know by any chance if I can remove and add in the same batch process? – Koen Heye Apr 11 at 8:13
  • I did not test this, but i believe it can – Fredric Foo Apr 11 at 9:46
1

As @Fredric mentioned you can do it by removing and adding the same contact identifier.

Example from working solution when we need to mark сontact identifier as 'Known' programmatically:

 var identifier = "[some client identifier]";

     var reference = new IdentifiedContactReference(Constants.XConnect.IdentificationSource, identifier);
     var contact = await xdbContext.GetContactAsync(reference, new ContactExpandOptions(
         PersonalInformation.DefaultFacetKey,
         EmailAddressList.DefaultFacetKey
     ));

...

if (!contact.IsKnown)
{
         var identifierToRemove = contact.Identifiers.FirstOrDefault(x => x.Source == Constants.XConnect.IdentificationSource);
         if (identifierToRemove != null)
         {
             var newIdentifier = new ContactIdentifier(Constants.XConnect.IdentificationSource, identifier, ContactIdentifierType.Known);
             xdbContext.RemoveContactIdentifier(contact, identifierToRemove);
             xdbContext.AddContactIdentifier(contact, newIdentifier);
         }
}

...

await xdbContext.SubmitAsync(cancellationToken);

-1

to update a contact you don't need to remove the contact indentifier, just check if the contact already exists and update the facet or create a new one if doesn't.

See the example bellow taken from Sitecore Documentation

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

                    if (existingContact != null)
                    {
                        // Retrieve facet by name
                        var facet = existingContact.GetFacet<EmailAddressList>(EmailAddressList.DefaultFacetKey);

                        if (facet != null)
                        {
                            // Change facet properties
                            facet.PreferredEmail = new EmailAddress("myrtle@test.test", true);
                            facet.PreferredKey = "Work";

                            // Set the updated facet
                            client.SetFacet(existingContact, EmailAddressList.DefaultFacetKey, facet);
                        }
                        else
                        {
                            // Facet is new
                            EmailAddressList emails = new EmailAddressList(new EmailAddress("myrtle@test.test", true), "Work");

                            client.SetFacet<EmailAddressList>(existingContact, EmailAddressList.DefaultFacetKey, emails);
                        }

                        client.Submit();
                    }

                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }
  • Where is the code above are you removing and replacing the identifier? The email address is not the identifier. It is just an email address. In the example above, a Twitter username is the identifier. IdentifiedContactReference("twitter", "myrtlesitecore") – Chris Auer Apr 10 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.