How do you add a record to an interest group?

Jul 1, 2011 at 11:03 PM

To add a list of multilple records to a given list I am using the following code snip. Everything is working except I cannot figure out the syntax to get a record added with a specific interest group selected:

var batch = new List<List.Merges>(); 

 

 

 

 

 

            while (customers.Read())
            {
                var contactName = customers["ContactName"].ToString().Split(new char[] { ' ' }, 2);

                if (contactName.Length == 1)
                {
                    Array.Resize(ref contactName, 2);
                    contactName[1] = "";
                }

                
                batch.Add(new List.Merges
                          {
                              
                              {"EMAIL", customers["Email"].ToString()},
                              {"FNAME", contactName[0]},
                              {"LNAME", contactName[1]},
                              {"CustomerID", customers["CustomerID"].ToString()},
                              {"Company", customers["CompanyName"].ToString()},
                              {"SalesRep", customers["Salesperson"].ToString()},
                              {"FirstOrder", customers["FirstOrder"].ToString()},
                              {"LastOrder", customers["LastOrder"].ToString()},
                              {"Services Purchased","Service A"}
                          });
                
            }
 

 

var options = new List.SubscribeOptions { DoubleOptIn = false, EmailType = List.EmailType.Html, UpdateExisting = true, ReplaceInterests = true};

var ret = chimp.ListBatchSubscribe(listId, batch, options);

 

 

I have the need to update, or add these members to certain interest groups. I have set the ReplaceInterests option to true. However, it is unclear how I set the interest group items for a specific record.

 

 

Coordinator
Jul 2, 2011 at 9:25 AM

Look at the second Merges constructor:

List.Merges(email, emailType, groupings)

Jul 4, 2011 at 1:03 AM

Thanks denisk -- however I am still having a problem getting this to work. Below is a snip of the code that is working great for adding and updating all records that are return for a SQL query that I am storing in a dataset, and running a foreach on. I just don't see where I should be entering the Group information, or in what format this information needs to be in. Any help would be appreciated.

           // Setup Subscribe Options that will be used for all records add or updated during the BatchListSubscribe
            var options = new List.SubscribeOptions { DoubleOptIn = false, EmailType = List.EmailType.Html, UpdateExisting = true, ReplaceInterests = true};

            
            var batch = new List<List.Merges>();
            var group = new List<List.InterestGroup>();
            
            // Do Database query
            string connectionString = @"Server=sql;Database=db_name;Integrated Security=true";
            var connection = new SqlConnection(connectionString);
            var cmd = new SqlCommand(@"SQL quey goes here"
                , connection);
            connection.Open();
            var customers = cmd.ExecuteReader();
            
            while (customers.Read())
            {
                var contactName = customers["ContactName"].ToString().Split(new char[] { ' ' }, 2);

                if (contactName.Length == 1)
                {
                    Array.Resize<string>(ref contactName, 2);
                    contactName[1] = "";
                }

                
                batch.Add(new List.Merges()
                          {
                              {"EMAIL", customers["Email"].ToString()},
                              {"FNAME", contactName[0]},
                              {"LNAME", contactName[1]},
                              {"CustomerID", customers["CustomerID"].ToString()},
                              {"Company", customers["CompanyName"].ToString()},
                              {"SalesRep", customers["Salesperson"].ToString()},
                              {"FirstOrder", customers["FirstOrder"].ToString()},
                              {"LastOrder", customers["LastOrder"].ToString()},
                          }
                          );

            }
            
            connection.Close();

            var ret = chimp.ListBatchSubscribe(listId, batch, options);

            Console.WriteLine("Email records Added:   {0}", ret.AddCount);
            Console.WriteLine("Email records Updated: {0}", ret.UpdateCount);
            Console.WriteLine("Email records Errors:  {0}\r\n", ret.ErrorCount);

            foreach (var error in ret.Errors)
            {
                Console.WriteLine("ERROR: {1}\r\n", error.Email, error.Message);
            }
Coordinator
Jul 4, 2011 at 7:47 AM

Try this, 

        static void Main(string[] args)
        {
            var chimp = new MCApi("<key>", false);
            string listId = "<listId>";

            // Setup Subscribe Options that will be used for all records add or updated during the BatchListSubscribe
            var options = new List.SubscribeOptions { DoubleOptIn = false, EmailType = List.EmailType.Html, UpdateExisting = true, ReplaceInterests = true };

            var batch = new List<List.Merges>();
            
            // Do Database query
            string connectionString = @"Server=sql;Database=db_name;Integrated Security=true";

            using (var connection = new SqlConnection(connectionString))
            {
                var cmd = new SqlCommand(@"SQL quey goes here", connection);
                connection.Open();
                var customers = cmd.ExecuteReader() as IDataReader;

                while (customers.Read())
                {
                    var contactName = customers["ContactName"].ToString().Split(new char[] { ' ' }, 2);
 
                    if (contactName.Length == 1)
                    {
                        Array.Resize<string>(ref contactName, 2);
                        contactName[1] = "";
                    }

                    var groupings = new List<List.Grouping>() 
                        {
                            new List.Grouping("G1", new string[] {"1","2","3","4"}),
                            new List.Grouping("G2", new string[] {"xxx","yyy"}),
                        };

                    batch.Add(new List.Merges(customers["Email"].ToString(), List.EmailType.Html, groupings)
                        {
                            {"FNAME", contactName[0]},
                            {"LNAME", contactName[1]},
                            {"CustomerID", customers["CustomerID"]},
                            {"Company", customers["CompanyName"]},
                            {"SalesRep", customers["Salesperson"]},
                            {"FirstOrder", customers["FirstOrder"]},
                            {"LastOrder", customers["LastOrder"]},
                        }
                    );

                }

                var ret = chimp.ListBatchSubscribe(listId, batch, options);

                Console.WriteLine("Email records Added:   {0}", ret.AddCount);
                Console.WriteLine("Email records Updated: {0}", ret.UpdateCount);
                Console.WriteLine("Email records Errors:  {0}\r\n", ret.ErrorCount);

                foreach (var error in ret.Errors)
                {
                    Console.WriteLine("ERROR: {1}\r\n", error.Email, error.Message);
                }
            }        
        }
Jul 4, 2011 at 2:18 PM

Thank you VERY MUCH!!!!! The code example you provided works perfectly.

By the way, you have inspired me to begin learning F# :-) Any resouces (training materials, books, etc.) that you have found that you would recommend?

Coordinator
Jul 4, 2011 at 3:48 PM
Edited Jul 4, 2011 at 3:49 PM

Gimme your email. i have couple of books...  they helped me to start :)

Jul 26, 2012 at 10:38 PM

Wow.

 

I think this is what I need as well.  Cool.

I just need a vb.net, F# or c# sample console app that will sync our MSSQL recs with our Mailchimp lists.

 

We have tried the diff wrappers.  no go.

 

Not enough docs.

 

Any idea (paid or free) of where we can get a project source of a console vb.net, F# or C# project

where we just have to add out MSSQL server credentials, db name, table and fields, etc.... and other manipulation

that will, add new users to a Mailchimp list, delete bounced or opt-out users from our MSSQL DB ?

 

Once we setup, we would  just run it from the command line.

Aaaaarrg!!! hhahahahahah.

 

Thx in advance.