/// <summary> /// This function will add a custom field to the given Constituent. /// It assumes your tenant has been configured with a text Constituent Attribute named "Custom Data Source Id". /// </summary> private async Task <string> AddCustomDataSourceIdToConstituent(Constituent constituent, CancellationToken cancellationToken) { CustomField cf = new CustomField(); cf.parent_id = constituent.id; cf.category = "Custom Data Source Id"; cf.value = constituent.custom_data_source_id; cf.comment = "Used to sync with the Custom Data Source"; cf.date = DateTime.Today; var request = new RestRequest("constituents/customfields", Method.POST); request.RequestFormat = DataFormat.Json; request.AddJsonBody(cf); var response = await _client.ExecuteTaskAsync(request, cancellationToken); if (response.StatusCode != HttpStatusCode.OK) { // Handle request errors here... Console.WriteLine($"ERROR: Add Constituent Cutom Field returned ({response.StatusCode})\r\n{response.Content}"); } var postResponse = JObject.Parse(response.Content); return(postResponse.Value <string>("id")); }
private async Task UpdateConstituent(Constituent constituent, CancellationToken cancellationToken) { // Set up the Rest Request to https://api.sky.blackbaud.com/constituent/<constituent_id> string requestUrl = string.Format("constituents/{0}", constituent.id); var request = new RestRequest(requestUrl, Method.PATCH); request.RequestFormat = DataFormat.Json; request.AddJsonBody(constituent); var response = await _client.ExecuteTaskAsync(request, cancellationToken); if (response.StatusCode != HttpStatusCode.OK) { // Handle request errors here... Console.WriteLine($"ERROR: Edit Constituent returned ({response.StatusCode})\r\n{response.Content}"); } }
private async Task <string> AddConstituent(Constituent constituent, CancellationToken cancellationToken) { var request = new RestRequest("constituents/", Method.POST); request.RequestFormat = DataFormat.Json; request.AddJsonBody(constituent); var response = await _client.ExecuteTaskAsync(request, cancellationToken); if (response.StatusCode != HttpStatusCode.OK) { // Handle request errors here... Console.WriteLine($"ERROR: Add Constituent returned ({response.StatusCode})\r\n{response.Content}"); } var postResponse = JObject.Parse(response.Content); return(postResponse.Value <string>("id")); }
public async Task SyncConstituentToRENXTAsync(Constituent constituent, CancellationToken cancellationToken) { try { var searchResults = await SearchForConstituent(constituent, cancellationToken); if (!searchResults.Any()) { // If the constituent cannot be found, add it RE NXT var newId = await AddConstituent(constituent, cancellationToken); constituent.id = newId; // Add any custom fields to the new constituent that are needed to keep your systems in sync await AddCustomDataSourceIdToConstituent(constituent, cancellationToken); } else { ConstituentSearchResult foundConstituent; if (searchResults.Count() == 1) { foundConstituent = searchResults.First(); } else { // If there are multiple search results, you may want to try to match the results on other data // In this example, we are going to use the Custom Data Source Id custom field foundConstituent = await FindConstituentInSearchResultsUsingCustomDataSourceId(constituent, searchResults, cancellationToken); } constituent.id = foundConstituent.id; await UpdateConstituent(constituent, cancellationToken); } } catch (TaskCanceledException) { } }
private async Task <ConstituentSearchResult> FindConstituentInSearchResultsUsingCustomDataSourceId(Constituent constituent, IEnumerable <ConstituentSearchResult> searchResults, CancellationToken cancellationToken) { foreach (ConstituentSearchResult result in searchResults) { var customFields = await GetCustomFieldsForConstituent(result, cancellationToken); var customDataSourceId = (from cf in customFields where cf.category.Equals("Custom Data Source Id") select cf).FirstOrDefault(); if (customDataSourceId != null && constituent.custom_data_source_id.Equals(customDataSourceId.value)) { return(result); } } // To keep this example simple, we are just going return the first search result if the custom field is not found return(searchResults.First()); }
private async Task <IEnumerable <ConstituentSearchResult> > SearchForConstituent(Constituent constituent, CancellationToken cancellationToken) { // Set up the Rest Request to https://api.sky.blackbaud.com/constituent/search?search_text=<email_address> string requestUrl = string.Format("constituents/search?search_text={0}", constituent.email_address); var request = new RestRequest(requestUrl, Method.GET); var response = await _client.ExecuteTaskAsync(request, cancellationToken); if (response.StatusCode != HttpStatusCode.OK) { // Handle request errors here... Console.WriteLine($"ERROR: Constituent Search returned ({response.StatusCode})\r\n{response.Content}"); } var collectionResponse = JObject.Parse(response.Content); var searchResults = collectionResponse["value"].ToObject <IEnumerable <ConstituentSearchResult> >(); return(searchResults); }