Sitecore Experience Platform

Access services from activity type class

The Services property allows you to access the following services from an activity:

  • xConnect Client API via Collection

  • Taxonomy Managers via Taxonomies

  • Definition Managers via Definitions

  • Reference Data Service Client API via Reference

  • Activity enrollment service via ActivityEnrollmentOperations

  • Conditiotion evalutation service via ConditionEvaluation (this service accepts an ICondition)

The following example demonstrates how to access each service:

using Sitecore.Xdb.MarketingAutomation.Core.Activity;
using Sitecore.Xdb.MarketingAutomation.Core.Processing.Plan;
using Microsoft.Extensions.Logging;
using System;
using Sitecore.Marketing.Taxonomy;
using Sitecore.Marketing.Definitions.Outcomes.Model;
using System.Linq;

namespace Documentation.Examples
{
    public class DoNothingActivity : IActivity
    {
        public string NoReplyAddress { get; set; }

        public IActivityServices Services { get; set; }

        public DoNothingActivity()
        {
        }

        public ActivityResult Invoke(IContactProcessingContext context)
        {
            // Get outcome definition manager
            var outcomeDefinitionManager = Services.Definitions.GetDefinitionManager<IOutcomeDefinition>();

            // Get outcome group taxonomy manager
            var outcomeGroupTaxonomyManager = Services.Taxonomies.GetManager<OutcomeGroupTaxonomyManager>();

            // Search other contacts
            var contacts = Services.Collection.Contacts.Where(x => x.Identifiers.Any(f => f.Source == "twitter"));

            // Search other interactions
            var interactions = Services.Collection.Interactions.Where(x => x.StartDateTime < DateTime.UtcNow);

            // Use reference data service
            var definition = Services.Reference.EnsureDefinitionType("mytype");

            // Enroll contact in plan without custom values
            Services.ActivityEnrollmentOperations.EnrollInPlan(context.Contact.Id.Value, Guid.NewGuid(), null);

            return new SuccessMove();
        }
    }
}

It is also possible to inject your own services.