Skip to main content

Campaigns

Campaign definitions are managed by the Sitecore.Marketing.Definitions.Campaigns.CampaignDefinitionManager class.

Accessing the CampaignDefinitionManager

The CampaignDefinitionManager is available from the Sitecore DI container. It is preferable to include a parameter of type DefinitionManagerBase<ICampaignActivityDefinition, CampaignActivityDefinitionRecord> in the constructor of your class and pull your class from the container, allowing the container to resolve the instance for you:

public MyClass(DefinitionManagerBase<ICampaignActivityDefinition, CampaignActivityDefinitionRecord> campaignDefinitionManager)
{
        ...
}

If you cannot use the container to construct your class, you can use the service locator. This class is also available in the Sitecore DI container:

using Sitecore.DependencyInjection;
using Sitecore.Marketing.Definitions;

ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<ICampaignActivityDefinition>();

Defining a campaign

A campaign is defined using types from the Sitecore.Marketing.Definitions.Campaigns.Model namespace.

using System;
using Sitecore.DependencyInjection;
using Sitecore.Marketing.Definitions;
using System.Globalization;
using Sitecore.Marketing.Definitions.Campaigns;

namespace Documentation
{
    public class DefineCampaign
    {
        public void Example()
        {
            Guid campaignActivityID = Guid.NewGuid(); // Campaign activity ID
            CultureInfo campaignActivityCulture = new CultureInfo("es"); // Campaign activity culture
            string campaignName = "Facebook Summer Campaign"; // Campaign activity name
            DateTime creationDate = DateTime.UtcNow; // Campaign activity creation date
            string createdBy = "sitecore\admin"; // Campaign activity creator

            CampaignActivityDefinition campaign = new CampaignActivityDefinition(campaignActivityID, "Campaign item name", campaignActivityCulture, campaignName, creationDate, createdBy);

            campaign.StartDate = DateTime.UtcNow;
            campaign.EndDate = DateTime.UtcNow.AddDays(20);
        }
    }
}

Note

The AutomationStateEnrollmentList will be removed in a later release as automation state enrollment has changed.

Saving a campaign

Once you have defined a campaign you may save it by calling the SaveAsync() method on the definition manager:

using System;
using Sitecore.DependencyInjection;
using Sitecore.Marketing.Definitions;
using System.Globalization;
using Sitecore.Marketing.Definitions.Campaigns;

namespace Documentation
{
    public class SaveCampaign
    {
        public void Example()
        {
            var definitionManager = ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<ICampaignActivityDefinition>();

            Guid campaignActivityID = Guid.NewGuid(); // Campaign activity ID
            CultureInfo campaignActivityCulture = new CultureInfo("es"); // Campaign activity culture
            string campaignName = "Facebook Summer Campaign"; // Campaign activity name
            DateTime creationDate = DateTime.UtcNow; // Campaign activity creation date
            string createdBy = "sitecore\admin"; // Campaign activity creator

            CampaignActivityDefinition campaign = new CampaignActivityDefinition(campaignActivityID, "Campaign item name", campaignActivityCulture, campaignName, creationDate, createdBy);

            campaign.StartDate = DateTime.UtcNow;
            campaign.EndDate = DateTime.UtcNow.AddDays(20);

            definitionManager.SaveAsync(campaign);
        }
    }
}

You can also optionally activate the campaign during save by passing true to the second parameter of the SaveAsync() method:

manager.SaveAsync(campaign, true);

Activating a campaign

Campaigns must be activated before they are available for use outside of management, such as being “published” out to the Reference Data Service.

Campaigns can be activated when they’re saved by passing true to the activate (second) parameter of the SaveAsync() method:

manager.SaveAsync(campaign, true);

Campaigns can also be activated without calling save, using the ActivateAsync() method:

manager.ActivateAsync(campaignId);

The ActivateAsync() method takes the ID of the campaign and does not require the campaign definition model.

Deleting a campaign

To delete a campaign, use the Delete() method on the manager. Individual cultures cannot be deleted from the definition, only the entire definition. The culture provided to the method call should be either null (default value) or CultureInfo.InvariantCulture.

manager.Delete(campaignActivityID);

Or:

manager.Delete(campaignActivityID, CultureInfo.InvariantCulture);

Retrieve a campaign

A single campaign can be retrieved by its ID using one of the Get() methods on the manager.

using System;
using Sitecore.DependencyInjection;
using Sitecore.Marketing.Definitions;
using System.Globalization;
using Sitecore.Marketing.Definitions.Campaigns;

namespace Documentation
{
    public class GetCampaign
    {
        public void Example()
        {
            var definitionManager = ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<ICampaignActivityDefinition>();

            Guid campaignActivityID = Guid.NewGuid(); // Campaign activity ID
            CultureInfo campaignActivityCulture = new CultureInfo("fr-fr"); // Campaign activity culture

            // Call Get() to get concrete campaign by ID and culture
            var campaignActivityDefinition = definitionManager.Get(campaignActivityID, campaignActivityCulture);
        }
    }
}

You can also choose to retrieve an inactive version if no active version exists:

var campaignActivityDefinition = manager.Get(campaignActivityID, campaignActivityCulture, true);

Retrieve a campaign by alias

You can also retrieve a campaign by its alias:

CultureInfo campaignActivityCulture = new CultureInfo("fr-fr"); // Campaign activity culture
var campaignActivityDefinitionByAlias = definitionManager.GetByAlias("My alias", campaignActivityCulture);

Update an existing campaign

To update an existing campaign definition, retrieve the campaign by ID, edit the campaign

Guid campaignActivityID = Guid.NewGuid(); // Campaign activity ID
CultureInfo campaignActivityCulture = new CultureInfo("es"); // Campaign activity culture

var campaignActivityToUpdate = manager.Get(campaignActivityID, campaignActivityCulture);
campaignActivityToUpdate.Name = "Updated campaign name";
manager.SaveAsync(campaignActivityToUpdate, true); // Save and activate

Retrieving all campaigns

The GetAll() method can be used to get all campaigns from the manager. As there may be a large number of definitions, this method supports paging. The return value is a single page of results.

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

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

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

To access the results, use the DataPage property of the result.

IAutomationCampaignDefinition campaign = page1AllCampaigns.DataPage.ElementAt(0);

The result also includes properties to expose the total number of definitions and the current page index and page size.

long totalDefinitionCount = page1AllCampaigns.Total;
int pageNumber = page1AllCampaigns.PageNumber;
int pageSize = page1AllCampaigns.Count;