Working with campaign templates programmatically

Abstract

Use campaign templates to streamline the campaign creation process.

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

Campaign templates are managed by 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 managed by the Sitecore.Marketing.Automation.Definitions.AutomationCampaignTemplates.AutomationCampaignTemplateDefinitionManager class

Task

 

Code

Access the automation campaign template manager. 

The AutomationCampaignTemplateDefinitionManager is available from the sitecore DI container.

Constructor Injection

public MyClass(IAutomationCampaignTemplateDefinitionManager manager){}

ServiceLocator

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 versionIAutomationPlanDefinition automationCampaingTemplate= manager.Get(automationCampaingTemplateId, new CultureInfo("da"));// Get by ID and culture. Will get the latest version, including if the version is inactiveIAutomationPlanDefinition automationCampaingTemplate = manager.Get(automationCampaingTemplateId, new CultureInfo("da"), true);// Get a specific version by ID, culture and version numberIAutomationPlanDefinition 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 onlyResultSet<DefinitionResult<IAutomationPlanDefinition>> campaignTemplates = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IAutomationPlanDefinition, string>());// Get page 2ResultSet<DefinitionResult<IAutomationPlanDefinition>> campaignTemplates = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IAutomationPlanDefinition, string>(pageNumber: 2));// Include inactive versionsResultSet<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 the AutomationCampaignTemplateDefinitionManager is called, or if the the SaveAsync is called and the activate parameter is set to true, a NotSupportedException occurs.