Esempio n. 1
0
        /// <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();
        }
Esempio n. 2
0
        /// <summary>
        /// Example of creating multiple contacts via batch. Also provides an
        /// example of updating existing contacts by merging upon an existing
        /// column (this will create a new contact if there are no merge
        /// candidates).
        ///
        /// Note that this is functionality is not backwards-compatible with
        /// version 1.1. Because merging upon a column could return one or
        /// more contact responses (if multiple merge candidates are found),
        /// ContactBatchResult now contains a ContactCollection object instead
        /// of a ContactEntity object. The example is updated to show the new
        /// usage.
        /// </summary>
        public void ContactBatchExample()
        {
            //Get the email and name column IDs. You should probably do validation on the response ;)
            string emailColumnID = api.ContactManager.GetColumnsByName("email").Columns[0].ID;
            string nameColumnID  = api.ContactManager.GetColumnsByName("name").Columns[0].ID;

            //Precreate a contact to test the 'upsert' merge functionality.
            Dictionary <string, string> fields = new Dictionary <string, string>();

            fields.Add(emailColumnID, "*****@*****.**");
            fields.Add(nameColumnID, "This name should be overwritten");

            ContactManager.Responses.ContactEntity contact = api.ContactManager.CreateContact(fields);

            Console.WriteLine("Created inital contact (ID: " + contact.ID + ") with name '" + contact.GetFieldsByName("name")[0].Value + "'.");

            List <ContactManager.Requests.ContactEntity> contacts = new List <ContactManager.Requests.ContactEntity>();

            for (int i = 0; i < 3; i++)
            {
                ContactManager.Requests.ContactEntity c = new ContactManager.Requests.ContactEntity();
                c.Fields = new ContactManager.Requests.FieldEntity[] {
                    new ContactManager.Requests.FieldEntity(emailColumnID, "example+" + i + "@example.com"),
                    new ContactManager.Requests.FieldEntity(nameColumnID, "Jane Doe")
                };

                contacts.Add(c);
            }

            Console.WriteLine("Submitting " + contacts.Count.ToString() + " contacts to a batch create.");

            //Supply the email column as the merge row.
            ContactManager.Responses.ContactBatchResponse response = api.ContactManager.BatchCreateContacts(contacts.ToArray(), emailColumnID);

            Console.WriteLine("Submitted batch job. ID is " + response.ID.ToString() + ".");

            // Wait for the batch to complete.
            do
            {
                response = api.ContactManager.GetBatchStatus(response.ID);

                Console.WriteLine("[" + System.DateTime.Now.ToLongTimeString() + "] Current status is: " + response.Status.ToString());

                System.Threading.Thread.Sleep(5000);
            } while (response.Status != ContactManager.Responses.ContactBatchResponse.BatchStatus.Complete &&
                     response.Status != ContactManager.Responses.ContactBatchResponse.BatchStatus.Error);

            ContactManager.Responses.ContactBatchResultCollection results = api.ContactManager.GetBatchResult(response.ID);
            foreach (ContactManager.Responses.ContactBatchResult result in results.Results)
            {
                //Output information about the created contacts, showing that we did indeed merge the
                //new contact information in. We're assuming that there is only one contact returned
                //as a result of each contact batch operation in the request, but there could be more.
                Console.WriteLine("Created / updated contact " + result.ContactCollection.Contacts[0].ID + " (" +
                                  result.ContactCollection.Contacts[0].GetFieldsByName("email")[0].Value + " / " +
                                  result.ContactCollection.Contacts[0].GetFieldsByName("name")[0].Value + ").");

                Console.WriteLine("Cleaning up: removing contact " + result.ContactCollection.Contacts[0].ID + ".");
                api.ContactManager.DeleteContact(result.ContactCollection.Contacts[0].ID);
            }

            Console.Write("Press enter to continue...");
            Console.ReadLine();
        }