3

I am working on Sitecore Analytics Contact code Upgrade from Sitecore 8.2 to 9.3

After upgrading latest dlls and adding XConnect assemblies, I am getting issue in some scenario

  1. LockAttemptResult is not present in Sitecore 9.3 analytics:

    LockAttemptResult<Contact> lockResult = 
        _contactRepository.TryLoadContact(
            contactIdentifier, 
            _leaseOwner, 
            TimeSpan.FromMinutes(1));
    if (lockResult.Status == LockAttemptStatus.AlreadyLocked)
    

Is there any replacement of this?

  1. LeaseOwner is not present in Sitecore.Analytics.Model:

     _leaseOwner = new LeaseOwner(requestorName, LeaseOwnerType.OutOfRequestWorker);
    

Is there any replacement of this class too?

1 Answer 1

1

You don't need to use in Sitecore 9 the LeaseOwner and lock/unlock contacts. With Sitecore xConnect it was simplified a lot how to send data to xDB.

In Sitecore 8+ to update data you have to use next code:

public void SetContactData(string username)
{
    LeaseOwner leaseOwner = new LeaseOwner("YOUR_WORKER_NAME", LeaseOwnerType.OutOfRequestWorker);
    ContactRepositoryBase contactRepository = Factory.CreateObject("contactRepository", true) as ContactRepositoryBase;
    // Attempt to obtain an exclusive lock on an existing contact in xDB.
    LockAttemptResult<Contact> lockResult = contactRepository.TryLoadContact(username, leaseOwner,
    TimeSpan.FromMinutes(1));
    Contact contact = null;
    if (lockResult.Status == LockAttemptStatus.AlreadyLocked)
    {
     // Another worker or a live web session has an exclusive lock on the contact.
     // You can't use this contact right now. It's up to you what to do in this case.
      /* ... */
     }
     else if (lockResult.Status == LockAttemptStatus.DatabaseUnavailable)
     {
      // Database is down. Try to handle this gracefully.
      /* ... */
     }
     else if (lockResult.Status == LockAttemptStatus.NotFound)
     {
        // A contact with the given identifier doesn't exist.
       // Just create a new contact object.
       contact = contactRepository.CreateContact(Guid.NewGuid());
      // Identify it.
       contact.Identifiers.Identifier = username;
       // And make it known.
       contact.Identifiers.IdentificationLevel = Sitecore.Analytics.Model.ContactIdentificationLevel.Known;
     }
     else
     {
       // We successfull locked an existing contact.
      contact = lockResult.Object;
     }
     // Set some contact facets:
     /* ... */
     // Save the contact and release the lock.
     if (contact != null)
     {
      var options = new ContactSaveOptions(release: true, owner: leaseOwner);
      contactRepository.SaveContact(contact, options);
      }
    }

with Sitecore 9 is pretty simple :

public void UpdateContact(string source, string identifier, Data data)
{
    using (XConnectClient client = GetClient())
     {
        IdentifiedContactReference reference = new IdentifiedContactReference(source, identifier);
        Contact contact = client.Get<Contact>(reference, new ContactExpandOptions(PersonalInformation.DefaultFacetKey));
        if (contact == null)
        {
            return;
        }
        // Set Facets
       client.SetFacet<PersonalInformation>(contact, PersonalInformation.DefaultFacetKey, personalInformation);
       client.Submit();
     }
}
4
  • Then how we insert our code in ContactRepositoryBase ? and what is source string user in parameters? May 4, 2021 at 7:34
  • what are you trying to do ? May 4, 2021 at 7:52
  • I am Upgrading my site form Sitecore 8.2 to 9.3, I am writing code to get, create and update contact. Just want to know how this Xconnect client will send contact to xdb. And one more thing what is this source parameter(any example if you can provide) May 5, 2021 at 7:54
  • so you need to use something similar with my code. xDB API was changed a lot in Sitecore 9, you need to reuse new code and to remove Sitecore 8 code. May 5, 2021 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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