public async Task <Client.Types.Contact> Get(string email) { Client.Types.Contact result = null; try { result = await _contact.GetByEmail(email); } catch (System.Net.Http.HttpRequestException ex) { _logger.LogError("HTTP request failed: {0}", ex.ToString()); _logger.LogError("Exception Type: "); _logger.LogError(ex.GetType().ToString()); _logger.LogError("Exception: " + ex.Message); if (ex.InnerException != null) { _logger.LogError("Inner exception is: {0}", ex.InnerException.GetType().ToString()); } } catch (Exception ex) { _logger.LogError("Non-HTTP exception captured."); _logger.LogError(ex.ToString()); } return(result); }
public async Task <IActionResult> OnGet(Guid id) { Console.WriteLine($"You requested {id}"); Stream response = await _crmClient.GetStreamAsync($"contacts({id})"); if (response == null) { return(NotFound("Contact does not exist.")); } Contact = CRMClient.DeserializeObject <Client.Types.Contact>(response); return(Page()); }
public async Task <IActionResult> Edit(Client.Types.Contact content) { if (ModelState.IsValid) { _logger.LogDebug($"To update Contact: {content.ID}"); Console.WriteLine($"your new department is: {content.Department}"); // Server side validation: how to tell what data are wrong? if (!string.IsNullOrEmpty(content.Department)) { // just update what we need, do not send everything back // https://msdn.microsoft.com/en-us/library/mt607664.aspx // Cannot use DataContract because selected properties to be updated. Really? JsonObject toUpdate = (JsonObject)JsonValue.Parse("{}"); toUpdate["department"] = content.Department; await contact.Update(content.ID, toUpdate); _logger.LogDebug($"Updated the Department to {content.Department} for {content.ID}"); } } return(await Edit(new Guid(content.ID))); }
/// <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); }