Manage marketing definitions programmatically
You can use the Marketing Operations API definition manager to manage your marketing definitions data, such as campaigns, goals, outcomes, funnels, and assets.
This topic includes code samples and describes how to:
Prepare Visual Studio
Prepare Visual Studio
To manage marketing definitions programmatically, in your Microsoft Visual Studio project:
-
Add the following project references:
-
Sitecore.Kernel -
Sitecore.Marketing -
Sitecore.Marketing.Core
-
-
Add the following namespaces:
-
using System.Globalization; -
using Sitecore.Marketing.Definitions; -
using Sitecore.Marketing.Definitions.Campaigns; -
using Sitecore.Data; -
using Sitecore.Marketing.Core;
-
Retrieve a marketing definition
Retrieve a marketing definition
If you are creating custom applications that do not have direct access to the Sitecore client UI, you can use the Marketing Operations API to retrieve marketing definitions data programmatically. For example, to customize Sitecore applications, such as the Experience Profile (xFile) or Experience Analytics reports, or when creating mobile applications. By default, the xFile only displays some basic information about recently triggered campaigns and you can retrieve more data using the Marketing Operations API.
-
To retrieve a marketing definition item, for example, a campaign definition item name, description, and taxonomic classification, adapt the following code sample:
The Get method of the definition manager returns null if the definition cannot be found. This can happen if the definition has been removed, or when the definition has not been pushed to the environment where the definition is being retrieved.
Create a marketing definition
Create a marketing definition
If you are creating custom applications that do not have direct access to the Sitecore client UI, create a new campaign definition item using the Marketing Operations API marketing definition manager.
To create a marketing definition:
-
Adapt the following code sample:
-
To activate a marketing definition, set the last parameter of the
SaveAsyncmethod totrue.When you activate a marketing definition:
-
The marketing definition is saved to the master database, published to the web database, and deployed to the reporting database.
-
The search indexes are updated (this applies to campaigns, assets, and funnels).
NoteIf you do not want to activate a marketing definition, set this parameter to
false. The marketing definition is only saved to the master database.This behavior is configurable and can be changed by modifying the
campaign/definitionManager/config/activationFeedsection in theSitecore.Marketing.configfile. -
Search marketing definitions
Search marketing definitions
You can use the Marketing Operations API to search for marketing definitions programmatically, for example, a campaign definition item. The search functionality only applies to campaigns, assets, and funnels.
-
To search marketing definitions, adapt the following code sample:
Delete a marketing definition
Delete a marketing definition
When you delete a marketing definition, for example, a campaign, it is deactivated and removed from the master, web, and reporting databases. It is also removed from the search indexes. This is the default behavior that applies to campaigns, assets, and funnels. You can configure this behavior by patching the <deleteCampaign> pipeline.
-
To delete a marketing definition item, adapt the following code sample: