locked
MailChimp API RRS feed

  • Question

  • User573357073 posted

    Hi,

    I'm sorry for starting this thread as i'm aware there's a similar thread available (http://forums.asp.net/p/1488091/4174248.aspx) however as i'm still a newbie and i have a very limited knowledge i tried to read and implement the documentation from http://perceptivemcapi.codeplex.com/ which can also be found from http://www.mailchimp.com/api/gettingstarted/, but i still couldn't get it to worked.

    The problem is i don't know where to start and where to put the wrapper, code, etc and with my limited knowledge i created this: Download File Here.

    In summary:

    1. firstly i created bin folder and put the CookComputing.XmlRpcV2.dll and PerceptiveMCAPI.dll there and reference them.
    2. I created App_Code folder and create 2 vb there with a name listBatchSubscribe.vb and Lists.vb and here are the codes:

    listBatchSubscribe.vb Code:

     

    Imports Microsoft.VisualBasic
    Imports PerceptiveMCAPI
    Imports PerceptiveMCAPI.Methods
    Imports PerceptiveMCAPI.Types
    Imports System.Configuration
    Imports System.Web
    
    Public Class listBatchSubscribe
    
        Public Sub listBatchSubscribe_method(ByVal SubscriberList As List(Of Subscriber))
            Dim input As listBatchSubscribeInput = New listBatchSubscribeInput()
            ' any directive overrides
            input.api_Validate = True
            input.api_AccessType = EnumValues.AccessType.Serial
            input.api_OutputType = EnumValues.OutputType.JSON
            ' method parameters
            input.parms.apikey = MCAPISettings.default_apikey
            input.parms.id = "YourListId"
            input.parms.double_optin = False
            input.parms.replace_interests = True
            input.parms.update_existing = True
            '
            Dim batch As List(Of Dictionary(Of String, Object)) = _
            New List(Of Dictionary(Of String, Object))
            For Each sub_rec As Subscriber In SubscriberList
                Dim entry As Dictionary(Of String, Object) = _
                New Dictionary(Of String, Object)
                entry.Add("EMAIL", sub_rec.email)
                entry.Add("EMAIL_TYPE", sub_rec.email_type)
                entry.Add("FNAME", sub_rec.first_name)
                entry.Add("LNAME", sub_rec.last_name)
                Dim next_payment As DateTime = sub_rec.last_payment.AddMonths(1)
                entry.Add("NEXTPAY", next_payment)
                batch.Add(entry)
            Next
            input.parms.batch = batch
            ' execution
            Dim cmd As listBatchSubscribe = New listBatchSubscribe(input)
            Dim output As listBatchSubscribeOutput = cmd.Execute()
            ' output, format with user control
            If output.api_ErrorMessages.Count > 0 Then
                ' raw data & errors
                showresults(output.api_Request, output.api_Response, _
                output.api_ErrorMessages, output.api_ValidatorMessages)
            Else
                show_listBatch1.Display(output)
            End If
        End Sub
    
    End Class

     

     

    Lists.vb Code:

     

    Imports Microsoft.VisualBasic
    Imports PerceptiveMCAPI
    Imports PerceptiveMCAPI.Methods
    Imports PerceptiveMCAPI.Types
    Imports System.Configuration
    Imports System.Web
    
    Public Class Lists
        ' A real simple request (using all default settings)
        Public Sub lists_method()
            ' input parameters, using default apikey
            Dim input As listsInput = New listsInput(MCAPISettings.default_apikey)
            ' execution
            Dim cmd As lists = New lists(input)
            Dim output As listsOutput = cmd.Execute()
            ' format output (Assuming a User control named show_lists)
            show_lists1.Display(output)
        End Sub
    End Class

     

     

    3. I put this vb code in the Default.aspx page:

     

    Imports PerceptiveMCAPI
    Imports PerceptiveMCAPI.Methods
    Imports PerceptiveMCAPI.Types
    Imports System.Configuration
    Imports System.Web
    
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim apikey As String = "myAPI-KEY" ' or default to config
            GridView1.DataSource = MCAPISettings.ListAPISettings().ToList()
            GridView1.DataBind()
        End Sub
    
    End Class

     

     

    4. And i add this code on the web.config

     

    <configSections>
          
          <section name="MailChimpAPIconfig" type="PerceptiveMCAPI.MCAPISettings, PerceptiveMCAPI"/>
          
        </configSections>
    
      <MailChimpAPIconfig>
        <MCAPI
        SecureAccess="False"
        Validate="False"
        DataCenter="auto"
        apikey="myAPI-KEY"
        delete_member="True"
        send_goodbye="False"
        double_optin="False"
        send_welcome="True" />
      </MailChimpAPIconfig>

     

    I know it still doesn't make sense here and there, however i truly have no idea and i'd really need your help.

    I truly appreciate if there's anyone who can give me a sample on how to implement it i'd truly appreciate it. I truly need a guideline as i've been spending a whole day yesterday trying to figure out and find any information that can help me but it still gets me nowhere.

    Basically what i'd like to achieve from the mailchimp API is an ability to get contact details from my sql server database and send an email to them right from my site.

    Hopefully i make myself clear and i truly appreciate your help and guideline.

    Thank you and kind regards,
    Stefanto 

    Thursday, May 5, 2011 9:00 PM

Answers

All replies

  • User573357073 posted

    Anyone?

    Sunday, May 15, 2011 8:50 PM
  • User-676557134 posted

    I am writing something almost exactly the same. Just posted in here too.

    Did you ever solve it?

    Here's what I have so far.

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using System.Data;
    using System.Data.OleDb;
    using System.IO;
    
    using PerceptiveMCAPI;
    using PerceptiveMCAPI.Methods;
    using PerceptiveMCAPI.Types;
    using PerceptiveMCAPI.Types.Internal;
    
    namespace MailChimpAPI_Csharp
    {
        class Program
        {
            static void Main(string[] args)
            {
                    //The following qstring find those who registered 
                    //but either never purchased or returned/voided/frauded and never re-purchased
                    string connectionString = GetConnectionString();
                    string queryString =
                        "SELECT DISTINCT Customer.Email, Customer.FirstName, Customer.LastName "+
                        "FROM Customer  " +
                        "WHERE NOT EXISTS " +
                        "(SELECT * FROM Orders  " +
                        "WHERE Customer.CustomerID = Orders.CustomerID AND  " +
                        "(Orders.RefundedOn IS NULL) AND (Orders.VoidedOn IS NULL) AND  " +
                        "(Orders.FraudedOn IS NULL) AND (Orders.AuthorizedOn IS NOT NULL)) AND  " +
                        "Customer.Email <>'' AND  " +
                        "Customer.Email is not null";
               
                    using (OleDbConnection connection =
                               new OleDbConnection(connectionString))
                    {
                        OleDbCommand command = connection.CreateCommand();
                        command.CommandText = queryString;
    
                        try
                        {
                            connection.Open();
    
                            OleDbDataReader reader = command.ExecuteReader();
    
                            //=========================================================================================================
                            //I know the following is all jumbled up. 
                            //I was using different source examples and got lost in it all..
                            //it's supposed to match  
                            //public void listBatchSubscribe_method(List<Subscriber> SubscriberList) near the bottom
                            //==========================================================================================================
                            using (reader)
                            {
                                System.Collections.Generic.List<MailChimpList> arrObjects = new System.Collections.Generic.List<MailChimpList>();
                                List<MailChimpList> student = new List<MailChimpList>();
                                int customerId = reader.GetOrdinal("customerId ");      //obviously this needs massaging...but how?
                                int CustomerName = reader.GetOrdinal("CustomerName ");  //obviously this needs massaging...but how?
    
                                while (reader.Read())
                                {
                                    CustomerEntity obj = new CustomerEntity();
                                    obj.customerId = (reader[customerId] != Convert.DBNull) ? reader[customerId].ToString() : null;
                                    obj.CustomerName = (reader[CustomerName] != Convert.DBNull) ? reader[CustomerName].ToString() : null;
                                    arrObjects.Add(obj);
                                }
    
                            }
                            //==================================================
    
                            while (reader.Read())
                            {
                            //build a list of users from the DB to update in listbatchsubscribe
                            }
                            reader.Close();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
    
    
            //=======================================================================================
            //then use this folowing listbatchsubscribe from PerceptiveMCAPI
            // A little more intense request (listBatchSubscribe using Serial/JSON)
            // assumes a collection was created elsewhere from database info
            // with email, email_type, first name, last name, and last_payment
    
            public void listBatchSubscribe_method(List<Subscriber> SubscriberList)
            {
                listBatchSubscribeInput input = new listBatchSubscribeInput();
                // any directive overrides
                input.api_Validate = true;
                input.api_AccessType = EnumValues.AccessType.Serial;
                input.api_OutputType = EnumValues.OutputType.JSON;
                // method parameters
                input.apikey = MCAPISettings.default_apikey;
                input.id = "YourListId";
                input.double_optin = false;
                input.replace_interests = true;
                input.update_existing = true;
                //
                List<Dictionary<string, object>> batch =
                new List<Dictionary<string, object>>();
                foreach (Subscriber sub_rec in SubscriberList)
                {
                    Dictionary<string, object> entry = new Dictionary<string, object>();
                    entry.Add("EMAIL", sub_rec.email);
                    entry.Add("EMAIL_TYPE", sub_rec.email_type);
                    entry.Add("FNAME", sub_rec.first_name);
                    entry.Add("LNAME", sub_rec.last_name);
                    DateTime next_payment = sub_rec.last_payment.AddMonths(1);
                    entry.Add("NEXTPAY", next_payment);
                    batch.Add(entry);
                }
                input.batch = batch;
                // execution
                listBatchSubscribe cmd = new listBatchSubscribe(input);
                listBatchSubscribeOutput output = cmd.Execute();
                // output, format with user control
                if (output.api_ErrorMessages.Count > 0)
                {
                    showResults(output.api_Request, output.api_Response, // raw data
                    output.api_ErrorMessages, output.api_ValidatorMessages); // & errors
                }
                else
                {
                    show_listBatch1.Display(output);
                }
            }
    
    
            static private string GetConnectionString()
            {
                // To avoid storing the connection string in your code, 
                // you can retrieve it from a configuration file.
                // Assumes Northwind.mdb is located in the c:\Data folder.
    
                //I will change this to use an external text file later================
                //TESTING ONLY!!!!!!!!!!!1=============================================
                return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
                    + "c:\\Data\\Northwind.mdb;User Id=admin;Password=;";
            }
    
    
        }
    }
    

    I had to change the project properties to use the full .NET 4.0 framework and not the 4 client profile... now the references work, but I'm getting errors in the name of the lists.
    example error for list 'subscriber'...

    Error    1    The type or namespace name 'Subscriber' could not be found (are you missing a using directive or an assembly reference?)    C:\Users\Beau D'Amore\Documents\Console Apps\MailChimpAPI-Csharp\MailChimpAPI-Csharp\Program.cs    90    52    MailChimpAPI-Csharp


    P.S. I'm writing it as a console app FYI

    Thursday, June 9, 2011 1:16 PM
  • User573357073 posted

    Hi Beaue, thanks for your help however i've posted a thread on the PercetiveMCAPI discussion board and jonHierro there helped me solved it.

    Here's the link to the discussion:
    http://perceptivemcapi.codeplex.com/discussions/257461

    Thanks :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 4, 2011 9:20 PM
  • User-676557134 posted

    Thanks! I've actually got it all working great. If you want some code, lemme know.

    Wednesday, July 6, 2011 9:14 AM
  • User670644709 posted

    Hi 

    Beau_Damore 

     i  want to know that if i want to check the "ClickStatus" "Total Number of Suscribers" etc etc than how could i add this functionality in my code:
    For now i have the following code:
     public bool SubscribeToList(string EmailAddress, string FirstName, string LastName, string Subject, string Body, string EmailFrom, string DisplayName)
           {
               listSubscribeInput input = new listSubscribeInput();
               input.api_AccessType = PerceptiveMCAPI.EnumValues.AccessType.Serial;
               input.api_CustomErrorMessages = true;
               input.parms.email_address = EmailAddress;
               input.parms.send_welcome = true;
               input.parms.update_existing = false;
               input.parms.replace_interests = true;
               input.parms.double_optin = false;
               input.parms.merge_vars.Add("FNAME", FirstName);
               input.parms.merge_vars.Add("LNAME", LastName);
               input.api_MethodType = PerceptiveMCAPI.EnumValues.MethodType.POST;
               input.api_Validate = true;
               input.api_OutputType = PerceptiveMCAPI.EnumValues.OutputType.XML;
               input.parms.apikey = Common.GetVariableByKey(VariableKey.MailChimpAPIKey); //System.Configuration.ConfigurationSettings.AppSettings["MailChimpApiKey"].ToString();
               input.parms.id = List();

               listSubscribe Subscribe = new listSubscribe();
               listSubscribeOutput output = Subscribe.Execute(input);

               if (output.api_ErrorMessages.Count > 0)
               {
                   ErrorCode = output.api_ErrorMessages.FirstOrDefault().code;
                   Error = "Error occured. " + output.api_ErrorMessages.FirstOrDefault().error;
                   return false;
               }

               return output.result;

           }

    Do you have a code for this???????

    Friday, October 21, 2011 6:01 AM