London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Shoma Gujjar
Nov 11, 2015
  3147
(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 Product Recommendation Troubleshooting

In today’s fast-paced digital landscape, personalization is everything . Customers expect relevant, tailored experiences whenever they interact wit...

Sanjay Kumar | Apr 28, 2025

Natural Language Q&A in Optimizely CMS Using Azure OpenAI and AI Search

In Part 2, we integrated Azure AI Search with Azure Personalizer to build a smarter, user-focused experience in Optimizely CMS. We used ServiceAPI ...

Naveed Ul-Haq | Apr 25, 2025 |

Identifying Spike Requests and Issues in Application Insights

Sometimes within the DXP we see specific Azure App Instances having request spikes causing performance degredation and we need to investigate. I fi...

Scott Reed | Apr 25, 2025

Optimizely Frontend Hosting Beta – Early Thoughts and Key Questions

Optimizely has opened the waitlist for its new Frontend Hosting capability. I’m part of the beta programme, but my invite isn’t due until May, whil...

Minesh Shah (Netcel) | Apr 23, 2025