Shoma Gujjar
Nov 11, 2015
  3024
(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
Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024

How to write a bespoke notification management system

Websites can be the perfect vehicle for notifying customers of important information quickly, whether it’s the latest offer, an operational message...

Nicole Drath | Sep 25, 2024