Ejemplo 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();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Metadata column management examples.
        /// </summary>
        public void MetadataColumnManagementExample()
        {
            //Create a new contact to test with.
            ContactManager.Responses.ColumnCollection columns = api.ContactManager.GetColumns();

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

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

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

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

            ContactManager.Responses.MetadataColumnCollection metadataColumns = api.ContactManager.GetMetadataColumns();
            Console.WriteLine("Retrieved all metadata columns.");

            //Add new metadata columns.
            if (metadataColumns.GetByName("Test Single Column") == null)
            {
                api.ContactManager.CreateMetadataColumn("Test Single Column", ContactManager.ContactManagerAPI.MetadataColumnType.SingleNumber);
            }
            if (metadataColumns.GetByName("Test Multi Column") == null)
            {
                api.ContactManager.CreateMetadataColumn("Test Multi Column", ContactManager.ContactManagerAPI.MetadataColumnType.MultiString);
            }
            if (metadataColumns.GetByName("Test Sum Column") == null)
            {
                api.ContactManager.CreateMetadataColumn("Test Sum Column", ContactManager.ContactManagerAPI.MetadataColumnType.SumNumber);
            }

            Console.WriteLine("Created metadata test columns.");

            //Update our single number field to have the value 2.
            ContactManager.Responses.MetadataFieldEntity singleColumn =
                api.ContactManager.UpdateContactMetadataField(contact.ID, metadataColumns.GetByName("Test Single Column").ID, new string[] { "2" });

            Console.WriteLine("Updated single column field with one integer.");
            Console.WriteLine("Value: " + singleColumn.Values[0]);

            //Update our multi-string field to have two values.
            ContactManager.Responses.MetadataFieldEntity multiColumn =
                api.ContactManager.UpdateContactMetadataField(contact.ID, metadataColumns.GetByName("Test Multi Column").ID, new string[] { "test multi value 1", "test multi value 2" });

            Console.WriteLine("Updated multi-valued field with two strings.");
            foreach (string value in multiColumn.Values)
            {
                Console.WriteLine("Value: " + value);
            }

            //"Sum number" fields are incrementers. Updating the field with a
            //number will actually add to the existing value, rather than
            //replacing it.
            ContactManager.Responses.MetadataFieldEntity sumColumn = new ContactManager.Responses.MetadataFieldEntity();
            for (int i = 0; i < 4; i++)
            {
                sumColumn = api.ContactManager.UpdateContactMetadataField(contact.ID, metadataColumns.GetByName("Test Sum Column").ID, new string[] { "1" });
                Console.WriteLine("Incremented sum number field by 1");
                Console.WriteLine("Value: " + sumColumn.Values[0]);
            }

            api.ContactManager.DeleteMetadataColumn(singleColumn.ID);
            api.ContactManager.DeleteMetadataColumn(multiColumn.ID);
            api.ContactManager.DeleteMetadataColumn(sumColumn.ID);

            Console.WriteLine("Removed test metadata columns.");

            api.ContactManager.DeleteContact(contact.ID);

            Console.WriteLine("Removed test contact " + contact.ID);

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