/// <summary>
        /// Examples of contact management resources.
        /// </summary>
        public void ContactManagementExample()
        {
            ContactManager.Responses.ListEntity list;

            //Create a testing list.
            try
            {
                list = api.ContactManager.CreateList("test list");
            }
            catch (APIException)
            {
                list = api.ContactManager.GetListsByName("test list").Lists[0];
            }

            Console.WriteLine("Created test list " + list.ID);

            //Retrieve our column mapping. We can use this to find out the
            //column IDs we'll need when setting contact fields.
            ContactManager.Responses.ColumnCollection columns = api.ContactManager.GetColumns();

            //Build up our contact data. Notice that we're using the column ID
            //as the field key, *not* the column name.
            Dictionary <string, string> fields = new Dictionary <string, string>();

            fields.Add(columns.GetByName("email").ID, "*****@*****.**");
            fields.Add(columns.GetByName("name").ID, "Test Contact");

            //Create a new contact, and assign them to the test list at the
            //same time.
            ContactManager.Responses.ContactEntity contact = api.ContactManager.CreateContact(fields, new int[] { list.ID });

            Console.WriteLine("Created contact " + contact.ID);

            //Attempt an 'upsert operation' on the created contact. The name
            //should be updated to the given value.
            Console.WriteLine("Attempting upsert (should update contact " + contact.ID + ").");

            fields[columns.GetByName("name").ID] = "Updated Test Contact";
            ContactManager.Responses.ContactCollection contacts = api.ContactManager.UpsertContact(fields, columns.GetByName("email").ID);

            foreach (ContactManager.Responses.ContactEntity c in contacts.Contacts)
            {
                Console.WriteLine("Updated contact " + c.ID + " (email address: " + c.GetFieldsByName("email")[0].Value + ") to have name '" + c.GetFieldsByName("name")[0].Value + "'.");
            }

            //Run a query on the contact database to retrieve our contact.
            //A contrived example, but an example nonetheless. The syntax
            //is detailed in the online documentation, but basically looks
            //something like: '`colID` = "value" AND `col2ID` = "other value"'
            string query = "`" + columns.GetByName("email").ID + "` = \"[email protected]\" AND `"
                           + columns.GetByName("name").ID + "` = \"Updated Test Contact\"";

            Console.WriteLine("Executing query: " + query);
            ContactManager.Responses.ContactCollection queryResult = api.ContactManager.GetContacts(0, 100, query);

            if (queryResult.Contacts.Length > 0)
            {
                Console.WriteLine("Ran search query and found " + queryResult.Contacts.Length + " contacts");
            }
            else
            {
                Console.WriteLine("Ran search query and found no contacts.");
            }

            //Retrieve a field by its name, and get the value from it.
            string email = contact.GetFieldsByName("email")[0].Value;

            Console.WriteLine("\tContact email: " + email);

            //Output each list that the contact belongs to (should only be our
            //test list).
            foreach (ContactManager.Responses.ListEntity l in contact.Lists)
            {
                Console.WriteLine("\tContact belongs to list " + l.ID);
            }


            //Change the contact's phone number.
            Dictionary <string, string> updateFields = new Dictionary <string, string>();

            updateFields.Add(columns.GetByName("phone").ID, "15555551234");

            contact = api.ContactManager.UpdateContact(contact.ID, updateFields);

            Console.WriteLine("Updated phone number: " + contact.GetFieldsByName("phone")[0].Value);

            //Add tags to a contact.
            ContactManager.Responses.MetadataColumnCollection metadataColumns = api.ContactManager.GetMetadataColumns();
            string metadataFieldID = metadataColumns.GetByName("Tags").ID;

            api.ContactManager.UpdateContactMetadataField(contact.ID, metadataFieldID, new string[] { "tag 1", "tag 2" });

            Console.WriteLine("Updated metadata tags");

            //Get metadata tags and display them.
            ContactManager.Responses.MetadataFieldEntity metadataField = api.ContactManager.GetContactMetadataFieldByName(contact.ID, "Tags").MetadataFields[0];
            Console.WriteLine("Retrieved metadata tags.");

            foreach (string value in metadataField.Values)
            {
                Console.WriteLine("\tMetadata tag: " + value);
            }

            //Delete our test list
            api.ContactManager.DeleteList(list.ID);

            Console.WriteLine("Deleted test list");

            //Refresh the contact and see if it still belongs to the list.
            contact = api.ContactManager.GetContact(contact.ID);

            Console.WriteLine("Contact belongs to " + contact.Lists.Length + " lists");


            api.ContactManager.DeleteContact(contact.ID);

            Console.WriteLine("Deleted contact.");

            Console.Write("Press enter to continue...");
            Console.Read();
        }
        /// <summary>
        /// Perform an upsert operation by either creating a contact or merging
        /// the given data into all contacts that match the provided merge
        /// column. For example, if the email field is provided in the fields
        /// parameter and is also specified as the merge column, all contacts
        /// with that email value will have their entries updated with the new
        /// data. If there is no match, a new contact will be created.
        /// </summary>
        /// <param name="fields">The fields to create / update.</param>
        /// <param name="mergeColumnID">The column ID to merge upon.</param>
        /// <returns>A collection containing all contacts modified by the
        /// <param name="listIDs">An array of list IDs to add the contact(s) to.
        /// </param>
        /// operation.</returns>
        public Responses.ContactCollection UpsertContact(Dictionary <string, string> fields, string mergeColumnID, int[] listIDs)
        {
            Dictionary <string, string> queryParameters = new Dictionary <string, string>();

            queryParameters.Add("mergecolumn", mergeColumnID);

            Requests.ContactEntity contact = new Requests.ContactEntity();

            Requests.FieldEntity[] fieldArray = new Requests.FieldEntity[fields.Count];
            int fieldCounter = 0;

            foreach (KeyValuePair <string, string> f in fields)
            {
                Requests.FieldEntity fieldEntity = new Requests.FieldEntity();
                fieldEntity.ID             = f.Key;
                fieldEntity.Value          = f.Value;
                fieldArray[fieldCounter++] = fieldEntity;
            }
            contact.Fields = fieldArray;

            if (listIDs.Length > 0)
            {
                int listCounter = 0;
                Requests.ListEntity[] listArray = new Requests.ListEntity[listIDs.Length];
                foreach (int id in listIDs)
                {
                    Requests.ListEntity listEntity = new Requests.ListEntity();
                    listEntity.ID            = id;
                    listArray[listCounter++] = listEntity;
                }
                contact.ListIds = listArray;
            }

            contact.ID = null;

            string xml = this.connection.Call <string>("POST", "contactmanager/contacts", queryParameters, contact);

            //We're going to deserialize ourselves, since the response could be
            //either a contact collection or a contact entity. We're going to
            //turn the contact entity into a collection for consistency.
            using (System.Xml.XmlReader reader = new System.Xml.XmlTextReader(new System.IO.StringReader(xml)))
            {
                //If it's a collection, return it.
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Responses.ContactCollection));
                if (serializer.CanDeserialize(reader))
                {
                    return((Responses.ContactCollection)serializer.Deserialize(reader));
                }

                serializer = new System.Xml.Serialization.XmlSerializer(typeof(Responses.ContactEntity));

                //If it's an entity, create a collection and put the contact in it.
                if (serializer.CanDeserialize(reader))
                {
                    Responses.ContactEntity     c = (Responses.ContactEntity)serializer.Deserialize(reader);
                    Responses.ContactCollection contactCollection = new Responses.ContactCollection();

                    contactCollection.Contacts = new Responses.ContactEntity[] { (Responses.ContactEntity)c };
                    return(contactCollection);
                }
                else
                {
                    throw new Exception("Invalid XML response: could not deserialize into a ContactEntity or ContactCollection.");
                }
            }
        }