Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Shoma Gujjar
Nov 11, 2015
  2939
(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
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog