Working with campaign templates programmatically

Current version: 9.3

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

RequestResponse
public MyClass(IAutomationCampaignTemplateDefinitionManager manager){}

ServiceLocator

RequestResponse
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.

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

Update an existing automation campaign template.

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

Delete an automation campaign template.

RequestResponse
manager.Delete(campaignTemplateId);

Retrieve an automation campaign template.

RequestResponse
// 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.

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

Retrieve all automation campaign templates.

RequestResponse
// 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.

Do you have some feedback for us?

If you have suggestions for improving this article,