November Happy Hour will be moved to Thursday December 5th.

Shoma Gujjar
Nov 11, 2015
  3053
(1 votes)

EPiServer Integration with DotMailer

EPiServer Integration with DotMailer

Hello,

This is very first attempt to write a blogpost but definitely not the last!

This blog intends to show DotMailer could be integrated into EPiserver using both Dotmailer’s SDK.

More documentation regarding DotMailer can be found here and some sample code can be found at gitHub .

 

Pre-requisites

1. Create a Dotmailer account if you do not have one.

2. Create a API user account. Information on how to get a API user key can be found here.

3. Comma separated CSV file

 

Integrating dotMailer Using SDK

Download  the SDK from here or using the nuget Package manager console command : Install-Package dotMailerSdk

Let’s get started : Using SDK

1. Create a static class helper class, i have called it DotMailerHelperClass

2. We first need to create a DmService using DmServiceFactory.Create()

private static DmService _dmFactoryService;
        public static DmService DmFactoryService
        {
            get { return _dmFactoryService ?? (_dmFactoryService = SetDmFactoryService()); }
            set { _dmFactoryService = value; }
        }       

        private static DmService SetDmFactoryService()
        {
            DmService dmFactoryService = DmServiceFactory.Create(UserName, Password);
            return dmFactoryService;
        }    

3. Once we have a DmService, we need to either import contacts to an existing address book or create a new address book to your account. This can be achieved using the DmFactoryService.AddressBookFactory class.

public static DmAddressBook UpdateExistingOrCreateNewAddressBook_UsingSDK(string addressBookName)
        {                       
            var addressBookList = DmFactoryService.AddressBookFactory.ListAddressBooks();
            var addressBook = addressBookList.FirstOrDefault(x => x.Name == addressBookName);

            if (addressBook != null)
            {
                return addressBook;
            }
            return DmFactoryService.AddressBookFactory.CreateAddressBook(addressBookName);                    
        }

4. Before we import contacts, we need to convert the csv file into an IEnumeble<IDmContact> format. The code below does the trick, just pass the path of the csv file:

public static IList<IDmContact> GetContactsForImport_ForSDK(string path)
        {           
            var reader =  new StreamReader(File.OpenRead(path));
            string format = "dd MMM yyyy hh:mm";
            IList<IDmContact> dmContacts = new List<IDmContact>();                       
            var readLine = reader.ReadLine();
            if (readLine != null)
            {
                var headers = readLine.Split(',');
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (line != null)
                    {
                        var values = line.Split(',');
                        DmContact dmContact = DmFactoryService.ContactFactory.CreateNewDmContact(values[0]);
                        for (int i = 1; i < headers.Count(); i++)
                        {
                            if (headers[i] == "DATE_FORMAT")
                            {
                                DateTime dateTime= values[i].ToDateTime();
                                dmContact[headers[i]] = dateTime.ToString(format);
                            }
                            else
                                dmContact[headers[i]] = values[i];
                        }                      
                        dmContacts.Add(dmContact);
                    }
                }
            }
           
            return dmContacts;
        }            

A couple of things are to be noted here:

  • If the csv file contains Date in string format, then this has to be converted into a proper date format.
  • The code also takes care of any custom data fields.
  • Custom fields for a DmContact can be accessed using properties.

Ex : dmContact[“Country”]

5. Import the IEnumerable<IDmContact> into the address book.

public static ImportContactResult ImportContacts_SDK(IEnumerable<IDmContact> contacts, int addressBookId)
        {           
            try
            {
                ImportContactOptions importContactOptions = new ImportContactOptions();
                var importContactResult =
                    DmFactoryService.AddressBookFactory.ImportContactsIntoAddressBook(importContactOptions,
                        addressBookId, contacts)
                        .GetImportWatcher().Watch();                                                                   
                importContactResult.GetImportWatcher().Dispose();
                return importContactResult;
            }
            catch (Exception exception)
            {
                Log.ErrorFormat("Exception in Importing contacts {0}",exception.Message);                 
            }
            return null;
        }     

A couple of things are to be noted here:

  • .GetImportWatcher().Watch();  waits for the import to complete.    GetImportWatcher() is a overloaded method where Timespan can also be specified to wait a particular duration of time.      
  • It’s the implementers responsibility to dispose the watch.    
  • The  ImportContactResult has information about the import status and importContactResult.ImportReport can be used to generate the report.

 

Hope that helps!

Thanks

Nov 11, 2015

Comments

K Khan
K Khan Nov 11, 2015 05:25 PM

Its quite helpful, Thanks for sharing!

Marija Jemuovic
Marija Jemuovic Nov 26, 2015 10:26 AM

Thx for sharing and keep them coming :)

Please login to comment.
Latest blogs
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog