By default when installing the Episerver Campaign connector its possible to configure the connection through admin mode. However in some occasions (such as in development and test scenarios) it may be desirable to configure these credentials through code.

This posts describes how to configure the Episerver Campaign Connector through code using an Episerver initialisation module: 

using System.Configuration;
using EPiServer.ConnectForCampaign.Implementation.Services;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;

namespace EPiServer.Reference.Commerce.Site.Features.CampaignInitialisation
{
    [InitializableModule, ModuleDependency(
               typeof(EPiServer.MarketingAutomationIntegration.InitializationModule))]
    public class InitCampaignFromConfig : IInitializableModule
    {
        public const string ConfigUsername = "campaign:Username";
        public const string ConfigPassword = "campaign:Password";
        public const string ConfigClientid = "campaign:Clientid";

        public void Initialize(InitializationEngine context)
        {
            // Check if the connector has been configured, if not look for settings in config
            if (CampaignSettingsRepository.Current.Password == null) 
            {
                // Look for config in the settings
                if (ConfigurationManager.AppSettings[ConfigUsername] != null &&
                    ConfigurationManager.AppSettings[ConfigPassword] != null &&
                    ConfigurationManager.AppSettings[ConfigClientid] != null)
                {
                    var campaignSettings = CampaignSettingsRepository.Current;
                    campaignSettings.UserName = 
                           ConfigurationManager.AppSettings[ConfigUsername];
                    campaignSettings.Password = 
                           ConfigurationManager.AppSettings[ConfigPassword];
                    campaignSettings.MandatorId = 
                           ConfigurationManager.AppSettings[ConfigClientid];

                    var repo = new CampaignSettingsRepository();
                    repo.Save(campaignSettings);
                }
            }
        }

        public void Uninitialize(InitializationEngine context) { }
    }
}

The outcome is that when starting a solution with a clean/default database that its possible to configure settings through code if they haven't been entered.


Comments

comments powered by Disqus