Shoma Gujjar
Nov 11, 2015
  2971
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024