﻿---
title: "Working with campaign templates programmatically"
slug: "working-with-campaign-templates-programmatically"
abstract: "Use campaign templates to streamline the campaign creation process."
---
# Working with campaign templates programmatically

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**
```csharp
public MyClass(IAutomationCampaignTemplateDefinitionManager manager){}
```

**Service locator**
```csharp
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](https://doc.sitecore.com/xp/en/developers/105/sitecore-experience-platform/automation-plans.html).

## Save an automation campaign template

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

## Update an existing automation campaign template

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

## Delete an automation campaign template

```csharp
manager.Delete(campaignTemplateId);
```

## Retrieve an automation campaign template

```csharp
// 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

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

## Retrieve all automation campaign templates

```csharp
// 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.