/// <summary> /// Demo of how to create and use AccountIDManager /// Used for getting parentaccountid when creating Contact /// </summary> /// <returns></returns> private async Task DemoAccountIDManager() { Client.Entities.Account account = new Client.Entities.Account(_crmClient); List <Client.Types.Account> accounts = await account.List(); foreach (var acc in accounts) { Console.WriteLine("{0} {1} {2}", acc.ID, acc.Name, acc.ParentAccountID); } Dictionary <string, string> accountDict = Utils.FromListToDict(accounts, "Name", "ID"); Console.WriteLine("Total count = {0}", accountDict.Count); foreach (var key in accountDict.Keys) { Console.WriteLine("{0} {1}", key, accountDict[key]); } Client.AccountIDManager manager = new AccountIDManager(accounts); Console.WriteLine(manager.GetAccountID("University of Adelaide", "School of Agriculture, Food & Wine")); }
/// <summary> /// Check AD user from a list if they exist in CRM as Contact, then try to create if they don't or update username if it is incorrect. /// </summary> /// <param name="newUsers"></param> /// <returns></returns> private async Task <(List <string>, List <string>, List <Dictionary <string, string> >)> CheckUserInCRM(List <User> newUsers) { Client.Entities.Account account = new Client.Entities.Account(_crmClient); Client.Entities.Contact contact = new Client.Entities.Contact(_crmClient); AccountIDManager manager = await account.GetAccountIDManager(); List <string> createdUsers = new List <string>(); List <string> unabledUsers = new List <string>(); List <Dictionary <string, string> > exceptionUsers = new List <Dictionary <string, string> >(); JsonObject ConvertADUserToJson(User user, string attachToAccountID) { return(Client.Types.ContactBase.Create(user.FirstName, user.LastName, user.Email, user.AccountName, user.Department, attachToAccountID)); } // Check the existence of a user by email address async Task CheckAndPrint(User user) { PrintUser(user); // FIXME: this may not work well in parallel by just print user details Client.Types.Contact result = await contact.GetByEmail(user.Email); if (result != null) { // Contact exists Console.WriteLine($"Found and the contact id = {result.ID}"); Log.Information($"{user.DistinguishedName}: found in CRM and the contact id = {result.ID}"); string contactID = result.ID; result = await contact.Get(new Guid(contactID)); // No username, need to set if (string.IsNullOrEmpty(result.Username)) { Log.Debug($"User name needed to be updated for contact {contactID}"); await contact.UpdateUsername(contactID, user.AccountName); Log.Information($"The username of Contact with id={contactID} has been set successfully."); } else { // found wrong username if (result.Username == user.AccountName) { Log.Debug($"contact {contactID} has username of {result.Username}"); } else { Log.Debug($"Contact {contactID} has username of {result.Username} which is different to samAccountName {user.AccountName}"); await contact.UpdateUsername(contactID, user.AccountName); Log.Information($"The username of Contact with id={contactID} has been updated successfully."); } } } else { // Contact does not exist Log.Debug($"{user.DistinguishedName} was not found in CRM"); string accountID = manager.GetAccountID(user.Company, user.Department); if (string.IsNullOrEmpty(accountID)) { Log.Warning($"Cannot create new Contact because cannot find accountID to attach for {user.DistinguishedName} could because of wrong department."); unabledUsers.Add(string.Format("{0} ({1})", user.DistinguishedName, user.Email)); } else { Log.Debug($"New contact will be created for {user.DistinguishedName}"); await contact.Create <JsonObject>(ConvertADUserToJson(user, accountID)); createdUsers.Add(user.DistinguishedName); Log.Information($"New contact has been created for {user.DistinguishedName}"); } } } Task[] taskArray = new Task[newUsers.Count]; int i = 0; foreach (var user in newUsers) { Log.Information("task {0}", i + 1); try { taskArray[i++] = await Task.Factory.StartNew(() => CheckAndPrint(user)); } catch (Exception ex) { exceptionUsers.Add(new Dictionary <string, string> { { "DistinguishedName", user.DistinguishedName }, { "Exception", ex.ToString() } }); Log.Error(ex, $"Failed attempt when processing {user.DistinguishedName}."); } } Task.WaitAll(taskArray); return(createdUsers, unabledUsers, exceptionUsers); }