1. Marketing Automation APIs

Working with campaign templates programmatically

Version:

The topic describes how to work with marketing automation campaign templates programmatically.

Campaign templates are managed by the Sitecore.Marketing.Automation.Definitions.AutomationCampaignTemplates.AutomationCampaignTemplateDefinitionManager class. They are stored in the master database under the following path: */sitecore/system/Marketing Control Panel/Marketing Automation Templates*

Access the automation campaign template manager

The AutomationCampaignTemplateDefinitionManager is available from the Sitecore DI container.

Constructor injection

public MyClass(IAutomationCampaignTemplateDefinitionManager manager){}

Service locator

IAutomationCampaignTemplateDefinitionManager manager = ServiceLocator.ServiceProvider.GetRequiredService<IAutomationCampaignTemplateDefinitionManager>();

Define an automation campaign template

Automation campaign templates and automation campaigns share the same structure and implement the IAutomationPlanDefinition. For more info, see Automation plans.

Save an automation campaign template

AutomationPlanDefinition campaignTemplate = CreateTemplate();
IAutomationCampaignTemplateDefinitionManager manager = ServiceLocator.ServiceProvider.GetRequiredService<IAutomationCampaignTemplateDefinitionManager>();
manager.SaveAsync(campaignTemplate);

Update an existing automation campaign template

AutomationPlanDefinition campaignTemplate = CreateTemplate();
IAutomationCampaignTemplateDefinitionManager manager = ServiceLocator.ServiceProvider.GetRequiredService<IAutomationCampaignTemplateDefinitionManager>();
manager.SaveAsync(campaignTemplate);
campaignTemplate.Name = "Updated Name";
manager.SaveAsync(campaignTemplate);

Delete an automation campaign template

manager.Delete(campaignTemplateId);

Retrieve an automation campaign template

// Get by ID and culture. Will get the latest active version
IAutomationPlanDefinition automationCampaingTemplate = manager.Get(automationCampaingTemplateId, new CultureInfo("da"));

// Get by ID and culture. Will get the latest version, including if the version is inactive
IAutomationPlanDefinition automationCampaingTemplate = manager.Get(automationCampaingTemplateId, new CultureInfo("da"), true);

// Get a specific version by ID, culture and version number
IAutomationPlanDefinition automationCampaingTemplate = manager.Get(automationCampaingTemplateId, new CultureInfo("da"), 3);

Retrieve an automation campaign template using an alias

CultureInfo culture = new CultureInfo("fr-fr");
var automatoinCampaignTemplate = definitionManager.GetByAlias("My alias", culture);

Retrieve all automation campaign templates

// Get All with defaults which will be first page, page size 20, latest active versions only
ResultSet<DefinitionResult<IAutomationPlanDefinition>> campaignTemplates = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IAutomationPlanDefinition, string>());

// Get page 2
ResultSet<DefinitionResult<IAutomationPlanDefinition>> campaignTemplates = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IAutomationPlanDefinition, string>(pageNumber: 2));

// Include inactive versions
ResultSet<DefinitionResult<IAutomationPlanDefinition>> campaignTemplates = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IAutomationPlanDefinition, string>(), true);
Note

You cannot activate an automation campaign template programatically. If the ActivateAsync method of the AutomationCampaignTemplateDefinitionManager is called, or if SaveAsync is called and the activate parameter is set to true, a NotSupportedException occurs.

If you have suggestions for improving this article, let us know!