1

I am trying to make sense of the dotMailer API for C#.

I have a class library where I intend to store the functionality that will consume the dotMailer API which references version 1.5 of the API. I also have a Service Reference set up from this WSDL

I was looking through the C# examples, but already I'm stumped! The following was pulled directly from here

Example of use in C#

/// <summary>
/// Adds a contact to an address book
/// </summary>
public void AddContactToAddressBook() 
{
    const string username = "apiuser-XXXXXXXXXXXX@apiconnector.com";
    const string password = "password";
    const int addressBookId = 1;  // ID of the target address book

    Console.WriteLine("AddContactToAddressBook");
    Console.WriteLine("-----------------------");

    // Get an instance to the web reference
    com.apiconnector.API api = new com.apiconnector.API();

    try
    {
        // we need a new contact
        com.apiconnector.APIContact contact = new com.apiconnector.APIContact();

        // populate the contact
        contact.AudienceType = com.apiconnector.ContactAudienceTypes.B2B;

        // populate the data fields
        contact.DataFields = new com.apiconnector.ContactDataFields();
        contact.DataFields.Keys = new string[3];
        contact.DataFields.Values = new object[3];

        contact.DataFields.Keys[0] = "FIRSTNAME";
        contact.DataFields.Values[0] = "John";
        contact.DataFields.Keys[1] = "LASTNAME";
        contact.DataFields.Values[1] = "Smith";
        contact.DataFields.Keys[2] = "POSTCODE";
        contact.DataFields.Values[2] = "IP4 1XU";


        // email address
        contact.Email = "joe.smith@example.com";

        contact.EmailType =  com.apiconnector.ContactEmailTypes.PlainText;
        contact.Notes = "This is a test only email";
        contact.OptInType = com.apiconnector.ContactOptInTypes.Single;

        // This method will create the contact required if it doesn't already exist within the dotMailer system,
        // so we don't have to call CreateContact as a prerequisite.
        // 
        // This method will also overwrite an existing contact, with the information provided here.
        //
        // This method will fail if you try to add a contact to the "Test" or "All Contacts" address books.
        //
        com.apiconnector.APIContact newContact = api.AddContactToAddressBook(username, password, contact, addressBookId);

        // Did we get something back from the API ?
        if (newContact != null)
        {
            Console.WriteLine("Contact added to address book {0} -> {1}", newContact.ID, addressBookId);
        }
    }
    catch (SoapException ex)  // catch any soap issues/errors from the web service
    {
        Console.WriteLine("Error -> {0}", ex.Message);                
    }

    Console.WriteLine();
}

My problem is that the following line does not resolve.

com.apiconnector.API api = new com.apiconnector.API();

I have looked in namespace dotMailer.Sdk.com.apiconnector for API but it does not exist, so where is it?

Am I missing something?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

1 Answers1

1

Add the wsdl as a service reference. In the example below I've called it "ServiceReference1" (because that's the default and I was lazy). You then use the reference to the APISoapClient (I've called it Client) instead of "api" that you're having trouble declaring.

All compiles fine, I'm not going to execute it because I've no idea what shenanigans my random code snippet is going to cause for the server! Should point you in the right direction?

using WindowsFormsApplication1.ServiceReference1;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const string username = "apiuser-XXXXXXXXXXXX@apiconnector.com";
        const string password = "password";
        const int addressBookId = 1;  // ID of the target address book


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddContactToAddressBook();
        }

        private void AddContactToAddressBook()
        {
            using (ServiceReference1.APISoapClient Client = new ServiceReference1.APISoapClient())
            {
                APIContact Contact = new APIContact();
                Contact.AudienceType = ContactAudienceTypes.B2B;

                APIContact NewContact = Client.AddContactToAddressBook(username, password, Contact, addressBookId); // etc. etc.



            }
        }
    }
}
Gareth
  • 2,746
  • 4
  • 30
  • 44
  • 1
    Nowhere does it say to use the APISoapClient class instead of API. Without this, I would still be on square one. Thank you sir!! – Matthew Layton Aug 03 '13 at 22:33