private void btnImportAddresses_Click(CommandBarButton ctrl, ref bool cancel) { Cursor.Current = Cursors.WaitCursor; try { // System.Data.DataSet ds = new System.Data.DataSet(); EmployeeTableAdapters.EmployeesTableAdapter td = new SyncMyAddress.EmployeeTableAdapters.EmployeesTableAdapter(); Employee.EmployeesDataTable ds = new Employee.EmployeesDataTable(); ds = td.GetData(); MAPIFolder contactsFolder = Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts); //Outlook.ContactItem contactItem; Outlook.Items contacts; contacts = contactsFolder.Items.Restrict("[MessageClass]='IPM.Contact'"); foreach (System.Data.DataRow dr in ds.Rows) { Outlook.ContactItem existingContact = (Outlook.ContactItem)contacts.Find("[Email1Address] = '" + dr["EmailID"] + "'"); if (existingContact != null) { existingContact.FirstName = (dr["FullName"] == null) ? string.Empty : dr["FullName"].ToString(); existingContact.CustomerID = (dr["EmpID"] == null) ? string.Empty : dr["EmpID"].ToString(); existingContact.JobTitle = (dr["Designation"] == null) ? string.Empty : dr["Designation"].ToString(); existingContact.Department = (dr["Department"] == null) ? string.Empty : dr["Department"].ToString(); existingContact.Email1Address = (dr["EmailID"] == null) ? string.Empty : dr["EmailID"].ToString(); existingContact.ManagerName = (dr["ManagerID"] == null) ? string.Empty : dr["ManagerID"].ToString(); existingContact.WebPage = "www.appsassociates.com"; existingContact.Save(); } else { Outlook.ContactItem newContact = Application.CreateItem(Outlook.OlItemType.olContactItem) as Outlook.ContactItem; newContact.FirstName = (dr["FullName"] == null) ? string.Empty : dr["FullName"].ToString(); newContact.CustomerID = (dr["EmpID"] == null) ? string.Empty : dr["EmpID"].ToString(); newContact.JobTitle = (dr["Designation"] == null) ? string.Empty : dr["Designation"].ToString(); newContact.Department = (dr["Department"] == null) ? string.Empty : dr["Department"].ToString(); newContact.Email1Address = (dr["EmailID"] == null) ? string.Empty : dr["EmailID"].ToString(); newContact.ManagerName = (dr["ManagerID"] == null) ? string.Empty : dr["ManagerID"].ToString(); newContact.WebPage = "www.appsassociates.com"; newContact.Save(); } } MessageBox.Show("Successfully Imported...!"); } catch (System.Exception ex) { MessageBox.Show("Sync Process is not completed. Error Message: " + ex.Message); } catch { MessageBox.Show("Sync Process is not completed"); } Cursor.Current = Cursors.Default; }
// exporting from outlook to SQL server private void btnExportAddresses_Click(CommandBarButton ctrl, ref bool cancel) { Cursor.Current = Cursors.WaitCursor; try { //Getting data from Database EmployeeTableAdapters.EmployeesTableAdapter td = new SyncMyAddress.EmployeeTableAdapters.EmployeesTableAdapter(); Employee.EmployeesDataTable ds = new Employee.EmployeesDataTable(); ds = td.GetData(); //Getting outlook contacts folder MAPIFolder contactsFolder = Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts); //Here i am getting only contacts from contacts folder. Outlook.Items contacts; contacts = contactsFolder.Items.Restrict("[MessageClass]='IPM.Contact'"); foreach (Outlook.ContactItem contact in contacts) { System.Data.DataRow [] existingContact = null; if (ds.Rows.Count > 0) { //Checking if the contact exists in database or not. existingContact = ds.Select("EmailID = '" + contact.Email1Address + "'"); } //if it exists, update if any information is updated inside if (existingContact != null && existingContact.Length > 0) { if (existingContact[0] != null) { System.Data.DataRow existsContact = existingContact[0]; existsContact["FullName"] = (contact.FullName == null) ? string.Empty : contact.FullName.ToString(); existsContact["Designation"] = (contact.JobTitle == null) ? string.Empty : contact.JobTitle.ToString(); existsContact["Department"] = (contact.Department == null) ? string.Empty : contact.Department.ToString(); existsContact["EmailID"] = (contact.Email1Address == null) ? string.Empty : contact.Email1Address.ToString(); existsContact["ManagerID"] = (contact.ManagerName == null) ? string.Empty : contact.ManagerName.ToString(); td.Update(existsContact); } } else { System.Data.DataRow newContact = ds.NewEmployeesRow(); newContact["FullName"] = (contact.FullName == null) ? string.Empty : contact.FullName.ToString(); newContact["Designation"] = (contact.JobTitle == null) ? string.Empty : contact.JobTitle.ToString(); newContact["Department"] = (contact.Department == null) ? string.Empty : contact.Department.ToString(); newContact["EmailID"] = (contact.Email1Address == null) ? string.Empty : contact.Email1Address.ToString(); newContact["ManagerID"] = (contact.ManagerName == null) ? string.Empty : contact.ManagerName.ToString(); ds.Rows.Add(newContact); ds.AcceptChanges(); td.Insert(contact.FullName, contact.JobTitle, contact.Email1Address, contact.Department, contact.ManagerName, DateTime.Today, string.Empty, string.Empty); } } MessageBox.Show("Successfully Exported...!"); } catch (System.Exception ex) { MessageBox.Show("Sync Process is not completed. Error Message: " + ex.Message); } catch { MessageBox.Show("Sync Process is not completed"); } Cursor.Current = Cursors.Default; }