1

I'm looking for a way to make it easier to find specific contacts in the experience profile. The entry in Experience Profile has a URL pattern

{Site Url}/sitecore/client/Applications/ExperienceProfile/contact?cid={contact id} 

I want to get contact id after submitting the form, so can anyone help me how to get the Contact ID?

1

To get XConnect Contact from Analytics Contact (visitor id) you can use method described in Sitecore documentation https://doc.sitecore.com/developers/100/sitecore-experience-platform/en/get-contacts.html, adapting it like so:

       try
                {
                    //get Contact Reference from Analytics contact id
                    var reference = new IdentifiedContactReference("xDB.Tracker", Tracker.Current.Contact.ContactId.ToString("N"));

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

                    Contact contact = await contactTask;
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
1

To get contact id , you need to use analytics tracker object and then connect with xconnect client. For this, you should add the below dll reference

  1. Sitecore.Analytics.Core.dll
  2. Sitecore.Analytics.dll
  3. Sitecore.Analytics.Model.dll
  4. Sitecore.Analytics.XConnect.dll
  5. Sitecore.XConnect.Client.Configuration.dll
  6. Sitecore.XConnect.Client.dll
  7. Sitecore.XConnect.Collection.Model.dll
  8. Sitecore.XConnect.Core.dll
  9. Sitecore.XConnect.dll
  10. Sitecore.XConnect.Segmentation.Predicates.dll

Here is the code reference to get contact id.

if (Sitecore.Analytics.Tracker.Current.Contact.IsNew)
        {

            var manager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as Sitecore.Analytics.Tracking.ContactManager;
            
            if (manager != null)
            {
                Sitecore.Analytics.Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
                
                manager.SaveContactToCollectionDb(Sitecore.Analytics.Tracker.Current.Contact);
                
                var trackerIdentifier = new IdentifiedContactReference(Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource, Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString("N"));
                
                using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
                {
                    var contact = client.Get<Contact>(trackerIdentifier, new Sitecore.XConnect.ContactExpandOptions());
                    
                    var contactid = contact.Id; // to get Contact id for new contact 
                }
            }
        }
        else
        {
            
            var anyIdentifier = Sitecore.Analytics.Tracker.Current.Contact.Identifiers.FirstOrDefault();
            
            using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                var contact = client.Get<Contact>(new IdentifiedContactReference(anyIdentifier.Source, anyIdentifier.Identifier), new Sitecore.XConnect.ContactExpandOptions(PersonalInformation.DefaultFacetKey));
                
                var contactid = contact.Id; // to get Contact id for existing contact 
            }
        }

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.