private static void SyncAccounts_Termed() { Account account = new Account(); var zavantaManagementService = new UserAndGroupManagment(strZavantaOnlineHost, strCurrentAccessToken, strCurrentRefreshToken); zavantaManagementService.TokenAcquired += ZavantaManagementService_TokenAcquired; Console.WriteLine("----------------------------------------------"); Console.WriteLine("Searching active directory for termed users..."); Console.WriteLine("----------------------------------------------"); using (var context = new PrincipalContext(ContextType.Domain, strAddress, strUserName, strPass)) { using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) { foreach (var r in searcher.FindAll()) { DirectoryEntry de = r.GetUnderlyingObject() as DirectoryEntry; if (de.Path.Contains(strTermOUs)) { if (de.Properties["mail"].Value != null) { account.Email = de.Properties["mail"].Value.ToString(); } else { account.Email = ""; } if (account.Email == "") { string firstName = de.Properties["givenName"].Value.ToString(); string lastName = de.Properties["sn"].Value.ToString(); listEmailNotifications.Add("Unable to delete " + firstName + " " + lastName + " because user record has no email address"); } try { zavantaManagementService.DeleteUser(email: account.Email); Console.WriteLine(" User " + account.Email + " deleted"); } catch (Exception ex) { Console.WriteLine(" Delete of " + account.Email + " user record failed (" + ex.Message + ")"); listEmailNotifications.Add("Delete of " + account.Email + " user record failed (" + ex.Message + ")"); } } } } } }
private static void SyncAccounts_New() { Account account = new Account(); var zavantaManagementService = new UserAndGroupManagment(strZavantaOnlineHost, strCurrentAccessToken, strCurrentRefreshToken); zavantaManagementService.TokenAcquired += ZavantaManagementService_TokenAcquired; DateTime today = DateTime.UtcNow.Date.AddDays(intMinusDays); Console.WriteLine("-------------------------------------------"); Console.WriteLine("Searching active directory for new users..."); Console.WriteLine("-------------------------------------------"); using (var context = new PrincipalContext(ContextType.Domain, strAddress, strUserName, strPass)) { using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) { foreach (var r in searcher.FindAll()) { DirectoryEntry de = r.GetUnderlyingObject() as DirectoryEntry; if (de.Properties["msExchVoiceMailboxID"].Value != null) { int empId = Convert.ToInt32(de.Properties["msExchVoiceMailboxID"].Value); if (empId != 0) { DateTime createdDate = Convert.ToDateTime(de.Properties["whenCreated"].Value); if (createdDate >= today) { if (de.Properties["givenName"].Value != null) { account.FirstName = de.Properties["givenName"].Value.ToString(); } else { account.FirstName = ""; } if (de.Properties["sn"].Value != null) { account.LastName = de.Properties["sn"].Value.ToString(); } else { account.LastName = ""; } if (de.Properties["department"].Value != null) { account.Department = de.Properties["department"].Value.ToString(); } else { account.Department = ""; } if (de.Properties["mail"].Value != null) { account.Email = de.Properties["mail"].Value.ToString(); } else { account.Email = ""; } if (de.Properties["title"].Value != null) { account.Position = de.Properties["title"].Value.ToString(); } else { account.Position = ""; } if (de.Properties["telephoneNumber"].Value != null) { account.Phone = de.Properties["telephoneNumber"].Value.ToString(); } else { account.Phone = ""; } try { zavantaManagementService.CreateUser(email: account.Email, firstName: account.FirstName, lastName: account.LastName, userType: Comprose.Zavanta.PublicApi.UserType.Reader, position: account.Position, department: account.Department, phone: account.Phone); Console.WriteLine(" User " + account.Email + " created"); } catch (Exception ex) { Console.WriteLine(" Create of " + account.Email + " user record failed (" + ex.Message + ")"); listEmailNotifications.Add("Create of " + account.Email + " user record failed (" + ex.Message + ")"); } } } } } } } }