Object used for encapsulating the details of a contact
コード例 #1
0
 public FrmUpdateContact(ContactDetails contact)
 {
     InitializeComponent();
     Contact = contact;
     TxtFirstName.Text = Contact.FirstName;
     TxtLastName.Text = Contact.LastName;
 }
        /// <summary>
        /// Method used for updating an existing contact
        /// </summary>
        /// <param name="credentialsDetails">An object that encapsulates the Constant Contact credentials</param>
        /// <param name="contact">The contact url</param>
        /// <param name="strRequest">The request string representation</param>
        /// <param name="strResponse">The response string representation</param>
        public static void UpdateExistingContact(CredentialsDetails credentialsDetails, ContactDetails contact, out string strRequest, out string strResponse)
        {
            string req;
            string res;

            //Get the contact data
            var xmlContact = ApiCallComponent.CallServiceGet(credentialsDetails, contact.Id, out req, out res);

            //Update the contact data
            var xContact = XDocument.Parse(xmlContact);
            var contactNode = (from f in xContact.Descendants(W3Ns + "content")
                               let element = f.Element(ConstantContactNs + "Contact")
                               where element != null
                               select element).FirstOrDefault();

            if (contactNode != null)
            {
                contactNode.SetElementValue(ConstantContactNs + "FirstName", contact.FirstName);
                contactNode.SetElementValue(ConstantContactNs + "LastName", contact.LastName);
            }

            //Put the updated contact data
            ApiCallComponent.CallServicePut(credentialsDetails, contact.Id, xContact.Declaration.ToString() + xContact, out strRequest, out strResponse);
        }
        /// <summary>
        /// Get the contacts for a given list along with some contact details
        /// </summary>
        /// <param name="credentialsDetail">An object that encapsulates the Constant Contact credentials</param>
        /// <param name="listId">The id of the list for witch the contacts will be brought</param>
        /// <param name="strRequest">The request string representation</param>
        /// <param name="strResponse">The response string representation</param>
        /// <returns>A list of contact detail objects</returns>
        public static List<ContactDetails> GetContacts(CredentialsDetails credentialsDetail, string listId,
            out string strRequest, out string strResponse)
        {
            var contactCollection = new List<ContactDetails>();

            var xmlResponse = ApiCallComponent.CallServiceGet(credentialsDetail, listId + "/members", out strRequest,
                                                              out strResponse);

            var xdoc = XDocument.Parse(xmlResponse);

            if (xdoc.Root == null)
            {
                return null;
            }

            var contacts = from f in xdoc.Root.Descendants(W3Ns + "entry")
                           let element = f.Element(W3Ns + "id")
                           where element != null
                           select new
                                      {
                                          id = "https" + Regex.Split(element.Value, "http")[1]
                                      };

            foreach (var contact in contacts)
            {
                string req;
                string res;

                var xmlContact = ApiCallComponent.CallServiceGet(credentialsDetail, contact.id, out req, out res);

                var contactDetails = new ContactDetails();

                var xContact = XDocument.Parse(xmlContact);

                var c = (from f in xContact.Descendants(W3Ns + "content")
                         let element = f.Element(ConstantContactNs + "Contact")
                         where element != null
                         let element0 = element.Element(ConstantContactNs + "EmailAddress")
                         where element0 != null
                         let element1 = element.Element(ConstantContactNs + "FirstName")
                         where element1 != null
                         let element2 = element.Element(ConstantContactNs + "LastName")
                         where element2 != null
                         let element3 = element.Element(ConstantContactNs + "InsertTime")
                         where element3 != null
                         select new
                                    {
                                        email = element0.Value,
                                        firstName = element1.Value,
                                        lastName = element2.Value,
                                        updated = element3.Value
                                    }).ToList();

                var firstOrDefault = c.FirstOrDefault();
                if (firstOrDefault != null)
                {
                    contactDetails.Id = contact.id;
                    contactDetails.FirstName = firstOrDefault.firstName;
                    contactDetails.LastName = firstOrDefault.lastName;
                    contactDetails.CreateDate = string.Format("{0:MMM d, yyyy}", DateTime.Parse(firstOrDefault.updated));
                    contactDetails.Email = firstOrDefault.email;
                }

                contactCollection.Add(contactDetails);
            }

            return contactCollection;
        }