1

I have created an Automation Plan and its enrollment is based on Custom Page Event.

The Plan is working as expected, but the contacts are not multiple-enrolled. Meaning that if the contact already in the plan, it will not enter it again until it exits.

In my case I want the contact to be enrolled again even if it's currently enrolled.

As per the documentation https://doc.sitecore.com/developers/93/sitecore-experience-platform/en/automation-plans.html I will need to Implement ContextKeyFactoryType to get unique enrollment key in order to allow multiple reentry.

The requirement is to get this key from custom contact facet which is being updated every week to allow the contact to be enrolled again in the plan once per week even if the custom Page Event raised more than once in the same week.

Now the GenerateKey (please see the snippet below) passes only the contactId and I want to load the contact and access its facets.

I have tried Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient() but it's not available in the Marketing Automation context.

 public class ContextKeyFactoryProvider : IContextKeyFactory
  {
    public string GenerateKey(Guid contactId, ExecutionData executionData)
    {      
      string str;
      /* Get XConnect Client to load the contact by contactId and access facets  */
      return str;
    }
  }

Note: I don't want to enroll the contact to the automation plan programmatically, and thus I will not be able to define ExecutionData CustomValues

2 Answers 2

0

We were able to get the xconnect contact by id using below code, hope this helps

public Contact GetContactById(Guid contactId, params string[] facets)
    {
      Assert.IsNotNull(contactId, "contactId is null");

      Contact contact = null;

      using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient())
      {
        try
        {
          contact = client.Get<Contact>(new ContactReference(contactId), new ContactExpandOptions(facets));
        }
        catch (XdbExecutionException ex)
        {
          Log.Error($"Was not able load the contact with the Id of {contactId}", ex, "Sitecore.XConnect.API");
        }
      }

      return contact;
    }

Contact is from the type Sitecore.XConnect.Contact

1
  • SitecoreXConnectClientConfiguration is not available in the Marketing Automation context. Your code will work fine in Sitecore context. but my question is about getting xconnect client from inside the Marketing Automation Engine in XConnect Mar 29, 2021 at 12:47
0

If DI works in your ContextKeyFactoryProvider you can inject IServiceProvider serviceProvider and load contact by using:

using (var scope = serviceProvider.CreateScope())
{
    using (var xdbContext= scope.ServiceProvider.GetService<IXdbContext>())
    {
        var contact = await xdbContext.GetContactAsync(...);
    }
}

otherwise you can do the same by using ServiceLocator:

var xdbContext = ServiceLocator.ServiceProvider.GetService<IXdbContext>();

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.