Example #1
0
        private void ExchangeContactsBtn_Click(object sender, EventArgs e)
        {
            var con = new SqlConnection("Data Source=192.160.0.120;Initial Catalog=zeebregtsdb;User ID=daan;Password=Bl22sk22k!");

            con.Open();
            SqlCommand command = new SqlCommand(@"SELECT persoon.voornaam,persoon.tussenvoegsel,persoon.achternaam,
                                                persoon.zaemail,persoon.telefoon_nr_1,persoon.telefoon_nr_2,
                                                persoon.telefoon_nr_3, persoon.telefoon_nr_settings, bedrijf.naam
                                                FROM persoon  INNER JOIN
                                                bedrijf ON persoon.bedrijf_nr = bedrijf.bedrijf_nr
                                                WHERE persoon.NIETactief = 'FALSE' ", con);
            var        reader  = command.ExecuteReader();

            var service = ExchangeComs.EWSFunctions.GetNewServiceHook();



            while (reader.Read())
            {
                try
                {
                    var cont = new ExchangeComs.ExchangeContactItem();

                    cont.Voornaam = reader[0] != null?String.IsNullOrEmpty(reader[0].ToString()) == false ? reader[0].ToString() : "" : "";

                    cont.Tussenvoegsel = reader[1] != null?String.IsNullOrEmpty(reader[1].ToString()) == false ? reader[1].ToString() : "" : "";

                    cont.Achternaam = reader[2] != null?String.IsNullOrEmpty(reader[2].ToString()) == false ? reader[2].ToString() : "" : "";

                    cont.Email1 = reader[3] != null?String.IsNullOrEmpty(reader[3].ToString()) == false ? reader[3].ToString() : "" : "";  // reader[12].ToString();

                    cont.TelNr1 = reader[4] != null?String.IsNullOrEmpty(reader[4].ToString()) == false ? reader[4].ToString() : "" : "";  //reader[20].ToString();

                    cont.TelNr2 = reader[5] != null?String.IsNullOrEmpty(reader[5].ToString()) == false ? reader[5].ToString() : "" : "";

                    cont.TelNr3 = reader[6] != null?String.IsNullOrEmpty(reader[6].ToString()) == false ? reader[6].ToString() : "" : "";

                    cont.BedrijfNaam = reader[8] != null?String.IsNullOrEmpty(reader[8].ToString()) == false ? reader[8].ToString() : "" : "";

                    //var contactId = ExchangeComs.EWSFunctions.FindContact(service, achternaam + "," + voornaam);
                    //if (contactId.ToString() != "0")
                    //{
                    //    ExchangeComs.EWSFunctions.DeleteContact(service, contactId);
                    //}
                    ExchangeComs.EWSFunctions.MakeNewContact(service, cont);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        public static void MakeNewContact(Microsoft.Exchange.WebServices.Data.ExchangeService service, ExchangeContactItem contactinfo)
        {
            var     f_id    = FindFolder(service);
            Contact contact = new Contact(service);

            Console.WriteLine("Making new contact: " + contactinfo.Achternaam + "," + contactinfo.Voornaam);
            // Specify the name and how the contact should be filed.
            contact.GivenName     = String.IsNullOrEmpty(contactinfo.Voornaam) == false ? contactinfo.Voornaam : "";
            contact.MiddleName    = String.IsNullOrEmpty(contactinfo.Tussenvoegsel) == false ? contactinfo.Tussenvoegsel : "";
            contact.Surname       = String.IsNullOrEmpty(contactinfo.Achternaam) == false ? contactinfo.Achternaam : "";
            contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName;

            // Specify the company name.
            contact.CompanyName = String.IsNullOrEmpty(contactinfo.BedrijfNaam) == false ? contactinfo.BedrijfNaam : "";

            // Specify the business, home, and car phone numbers.
            var phone1type = PhoneNumberKey.PrimaryPhone;
            var phone2type = PhoneNumberKey.MobilePhone;
            var phone3type = PhoneNumberKey.BusinessPhone;

            if (!String.IsNullOrEmpty(contactinfo.TelNrTypes))
            {
                var sets = contactinfo.TelNrTypes.Split(',');
                var nr1  = int.Parse(sets[0].Substring(0, 1));
                var nr2  = int.Parse(sets[1].Substring(0, 1));
                var nr3  = int.Parse(sets[2].Substring(0, 1));
                if (nr2 == nr1)
                {
                    nr2 = 6;
                }
                if (nr3 == nr2 || nr3 == nr1)
                {
                    nr3 = 7;
                }

                phone1type = ConvertTelTypes(nr1);
                phone2type = ConvertTelTypes(nr2);
                phone3type = ConvertTelTypes(nr3);
            }

            contact.PhoneNumbers[phone1type] = String.IsNullOrEmpty(contactinfo.TelNr1) == false ? contactinfo.TelNr1 : "";
            contact.PhoneNumbers[phone2type] = String.IsNullOrEmpty(contactinfo.TelNr2) == false ? contactinfo.TelNr2 : "";
            contact.PhoneNumbers[phone3type] = String.IsNullOrEmpty(contactinfo.TelNr3) == false ? contactinfo.TelNr3 : "";

            // Specify email addresses.
            contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress(String.IsNullOrEmpty(contactinfo.Email1) == false ? contactinfo.Email1 : "*****@*****.**");


            //functie
            contact.JobTitle = String.IsNullOrEmpty(contactinfo.Functie) == false ? contactinfo.Functie : "";
            // Save the contact.
            contact.Save(f_id);
        }
        public static void UpdateContact(Microsoft.Exchange.WebServices.Data.ExchangeService service, ItemId contactUniId, ExchangeContactItem contactinfo)
        {
            // Bind to an existing meeting request by using its unique identifier.
            Contact contact = Contact.Bind(service, contactUniId);

            // Specify the name and how the contact should be filed.
            if (String.IsNullOrEmpty(contactinfo.Voornaam) == false && contact.GivenName != contactinfo.Voornaam)
            {
                contact.GivenName = contactinfo.Voornaam;
            }
            if (String.IsNullOrEmpty(contactinfo.Tussenvoegsel) == false && contact.MiddleName != contactinfo.Tussenvoegsel)
            {
                contact.MiddleName = contactinfo.Tussenvoegsel;
            }
            if (String.IsNullOrEmpty(contactinfo.Achternaam) == false && contactinfo.Achternaam != contact.Surname)
            {
                contact.Surname = contactinfo.Achternaam;
            }

            // Specify the company name.
            if (String.IsNullOrEmpty(contactinfo.BedrijfNaam) == false && contact.CompanyName != contactinfo.BedrijfNaam)
            {
                contact.CompanyName = contactinfo.BedrijfNaam;
            }


            // Specify the phone numbers.

            var phone1type = PhoneNumberKey.PrimaryPhone;
            var phone2type = PhoneNumberKey.MobilePhone;
            var phone3type = PhoneNumberKey.BusinessPhone;

            if (!String.IsNullOrEmpty(contactinfo.TelNrTypes))
            {
                var sets = contactinfo.TelNrTypes.Split(',');
                var nr1  = int.Parse(sets[0]);
                var nr2  = int.Parse(sets[1]);
                var nr3  = int.Parse(sets[2]);
                if (nr2 == nr1)
                {
                    nr2 = 6;
                }
                if (nr3 == nr2 || nr3 == nr1)
                {
                    nr3 = 7;
                }

                phone1type = ConvertTelTypes(nr1);
                phone2type = ConvertTelTypes(nr2);
                phone3type = ConvertTelTypes(nr3);
                if (phone1type == PhoneNumberKey.BusinessFax)
                {
                    contactinfo.TelNr1 = "Fax:" + contactinfo.TelNr1;
                    phone1type         = PhoneNumberKey.Pager;
                }
                if (phone2type == PhoneNumberKey.BusinessFax)
                {
                    contactinfo.TelNr2 = "Fax:" + contactinfo.TelNr2;
                    phone2type         = PhoneNumberKey.Pager;
                }

                if (phone3type == PhoneNumberKey.BusinessFax)
                {
                    contactinfo.TelNr3 = "Fax:" + contactinfo.TelNr3;
                    phone3type         = PhoneNumberKey.Pager;
                }
            }

            if (String.IsNullOrEmpty(contactinfo.TelNr1) == false)
            {
                if (contact.PhoneNumbers.Contains(phone1type) == true)
                {
                    if (contact.PhoneNumbers[phone1type] != contactinfo.TelNr1)
                    {
                        contact.PhoneNumbers[phone1type] = contactinfo.TelNr1;
                    }
                }
                else
                {
                    contact.PhoneNumbers[phone1type] = contactinfo.TelNr1;
                }
            }
            if (String.IsNullOrEmpty(contactinfo.TelNr2) == false)
            {
                if (contact.PhoneNumbers.Contains(phone2type) == true)
                {
                    if (contact.PhoneNumbers[phone2type] != contactinfo.TelNr2)
                    {
                        contact.PhoneNumbers[phone2type] = contactinfo.TelNr2;
                    }
                }
                else
                {
                    contact.PhoneNumbers[phone2type] = contactinfo.TelNr2;
                }
            }
            if (String.IsNullOrEmpty(contactinfo.TelNr3) == false)
            {
                if (contact.PhoneNumbers.Contains(phone3type) == true)
                {
                    if (contact.PhoneNumbers[phone3type] != contactinfo.TelNr3)
                    {
                        contact.PhoneNumbers[phone3type] = contactinfo.TelNr3;
                    }
                }
                else
                {
                    contact.PhoneNumbers[phone3type] = contactinfo.TelNr3;
                }
            }


            // Specify two email addresses.
            contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress(String.IsNullOrEmpty(contactinfo.Email1) == false ? contactinfo.Email1 : "*****@*****.**");

            //save the changes
            contact.Update(ConflictResolutionMode.AlwaysOverwrite);
        }