danesparza / MailChimp.NET

:envelope: .NET Wrapper for the MailChimp v2.0 API
MIT License
179 stars 119 forks source link

Batchsubscribe does not appear to be updating MailChimp #112

Closed shanebekker closed 10 years ago

shanebekker commented 10 years ago

Hi,

I know I am doing something wrong but can not find out what. I am storing, atm rather rough, subscribers in a database before (clients request) attempting to batch upload them into MailChimp.

I am not clear on how the Mergvars work so I think I have messed that up.

Here is my rough initial code:

       MailChimpManager mcm = new MailChimpManager(user.MailChimpAPIKey);
        List<BatchEmailParameter> listOfEmails = new List<BatchEmailParameter>();

        var data = from d in db.Subscribers
                   where d.ListID == id
                   select d;

        List<string> listSubscriber = new List<string>();
        char[] de = new char[] { ',' };
        foreach (var d in data)
        {
            string sub = d.Data;
            listSubscriber = sub.Split(de, StringSplitOptions.RemoveEmptyEntries).ToList();

            List<string> listid = new List<string>();
            listid.Add(id);

            MergeVarResult mvr = mcm.GetMergeVars(listid);

            MergeVarListResult mvlr = mvr.Data[0];
            List<MergeVarItemResult> mvir = mvlr.MergeVars;

            Dictionary<string, string> mergvar = new Dictionary<string, string>();

            int i = 0;

            foreach (var item in mvir)
            {
                var r = item.Tag;
                mergvar.Add(item.Tag, listSubscriber[i]);
                i++;
            }

            EmailParameter email = new EmailParameter()
            {
                Email = mergvar.Where(m => m.Key == "EMAIL").First().ToString()

            };

            BatchEmailParameter MCEmail = new BatchEmailParameter() { Email = email, EmailType = "html", MergeVars = mergvar };

            listOfEmails.Add(MCEmail);
        }

        try
        {
            mcm.BatchSubscribe(id, listOfEmails, doubleOptIn: false, updateExisting: true,    replaceInterests: true);
            mcm.Ping();

        }
        catch (Exception ex)
        {
            var error = ex.Message;
        }
shanebekker commented 10 years ago

As I received no answer to his query I decided to just loop over the results and call the Subscribe function one at a time as a work around

shanebekker commented 10 years ago

Hi Dan,

While I am sure we are all mostly thankful for your effort at providing this fine Wrapper this issue is not actually closed as all I did was provide my work around but it is not the solution.

A proper solution should only then allow this to be closed, do you not agree?

mdissel commented 10 years ago

I've updated the test in my fork, see https://github.com/mdissel/MailChimp.NET/commit/b2a391cf4db81972f7f6b58f731ade272e8fb263

ps. without the replacement of the type object to MergeVar it was also working.

shanebekker commented 10 years ago

Thanks for your reply I have created one list in mailchimp called kmp Here is an API key do you can see for yourself d84007881f268a83bf7755e984e3f012-us9 It will only be available for the next 24 hrs for obvious reasons

Thanks

-----Original Message----- From: "mdissel" notifications@github.com Sent: ‎25/‎09/‎2014 08:35 To: "danesparza/MailChimp.NET" MailChimp.NET@noreply.github.com Cc: "Shane Bekker" shane.bekker@gmail.com Subject: Re: [MailChimp.NET] Batchsubscribe does not appear to be updatingMailChimp (#112)

I've updated the test in my fork, see mdissel@b2a391c ps. without the replacement of the type object to MergeVar it was also working. — Reply to this email directly or view it on GitHub.=

mdissel commented 10 years ago

Please copy the test into your own code and try it out. The main difference is that i'm using a MergeVar class for the field variables and you're only using a dictionary.. I create a pull request to update the type to MergeVar instead of object.

shanebekker commented 10 years ago

Well the issue will be that my code does not know what the mailchimp fields will be so I cannot create a static class as I do know what the cards will be until I query the list id

-----Original Message----- From: "mdissel" notifications@github.com Sent: ‎25/‎09/‎2014 08:51 To: "danesparza/MailChimp.NET" MailChimp.NET@noreply.github.com Cc: "Shane Bekker" shane.bekker@gmail.com Subject: Re: [MailChimp.NET] Batchsubscribe does not appear to be updatingMailChimp (#112)

Please copy the test into your own code and try it out. The main difference is that i'm using a MergeVar class for the field variables and you're only using a dictionary.. I create a pull request to update the type to MergeVar instead of object. — Reply to this email directly or view it on GitHub.=

mdissel commented 10 years ago

Just leave it empty if you don't want to set a field with a specific value. See the MailChimp API documentation for more information.