static void RetrieveUsers() { Console.WriteLine("Start to sync AAD Users ..."); AllUsers.Clear(); IUserCollection userCollection = activeDirectoryClient.Users; IPagedCollection<IUser> searchResults = activeDirectoryClient.Users.OrderBy(user => user.DisplayName).ExecuteAsync().Result; List<IUser> usersList = searchResults.CurrentPage.ToList(); if (usersList != null && usersList.Count > 0) { int count = 1; do { AllUsers.AddRange(usersList); Console.WriteLine("===={0}===", count++); usersList = searchResults.CurrentPage.ToList(); newUserList = usersList; foreach (IUser user in usersList) { //Console.WriteLine("User DisplayName: {0} UPN: {1}", user.DisplayName, user.UserPrincipalName); var newUser = new UserEntity(user.ObjectId, user.UserPrincipalName.Contains("#EXT#") ? user.UserPrincipalName.Replace("#EXT#", "_") : user.UserPrincipalName); newUser.DisplayName = user.DisplayName; newUser.MailNickname = user.MailNickname; newUser.AccountEnabled = (bool)user.AccountEnabled; newUser.UserType = user.UserType; newUser.UsageLocation = user.UsageLocation; userTableOper.InsertEntity(newUser); } searchResults = searchResults.GetNextPageAsync().Result; } while (searchResults != null); // && searchResults.MorePagesAvailable } }
static void RetrieveUpdatedUser(string userUPN) { // search for a single user by UPN User retrievedUser = new User(); List<IUser> retrievedUsers = null; try { retrievedUsers = activeDirectoryClient.Users.Where(user => user.UserPrincipalName.Equals(userUPN)).ExecuteAsync().Result.CurrentPage.ToList(); } catch (Exception e) { Console.WriteLine("\nError getting new user {0} {1}", e.Message, e.InnerException != null ? e.InnerException.Message : ""); } // should only find one user with the specified UPN if (retrievedUsers != null && retrievedUsers.Count == 1) { retrievedUser = (User)retrievedUsers.First(); var newUser = new UserEntity(retrievedUser.ObjectId, retrievedUser.UserPrincipalName.Contains("#EXT#") ? retrievedUser.UserPrincipalName.Replace("#EXT#", "_") : retrievedUser.UserPrincipalName); newUser.DisplayName = retrievedUser.DisplayName; newUser.MailNickname = retrievedUser.MailNickname; newUser.AccountEnabled = (bool)retrievedUser.AccountEnabled; newUser.UserType = retrievedUser.UserType; newUser.UsageLocation = retrievedUser.UsageLocation; userTableOper.InsertEntity(newUser); } }
public bool InsertEntity(UserEntity user) { var insertOperation = TableOperation.InsertOrReplace(user); _table.Execute(insertOperation); return true; }