public void UpdateEntity(string entityName, Guid id, string entityData) { LogInformation($"[{nameof(UpdateEntity)}] --> {entityName}/{entityData}"); ApplyAccessToken(); HttpResponseMessage response; var method = new HttpMethod("PATCH"); var content = new StringContent(entityData, Encoding.UTF8, "application/json"); var requestUri = new Uri($"{_httpClient.BaseAddress.AbsoluteUri}{entityName}({id})"); var request = new HttpRequestMessage(method, requestUri) { Content = content }; response = _httpClient.SendAsync(request).Result; if (response.StatusCode != HttpStatusCode.NoContent) { var ex = new CrmApiException(response.ReasonPhrase, response.StatusCode); _logger.LogError(ex, $"[{ex.HttpStatus}] + [{ex.Message}]"); throw ex; } }
private void UpdateField(string entityName, string entityField, Guid entityId, string value) { var address = entityName + "(" + entityId + ")/" + entityField; //Don't add quotes if value is a bool/int bool boolField; int intField; string updateBody; if (bool.TryParse(value, out boolField) || int.TryParse(value, out intField)) { updateBody = "{\"value\": " + value.ToLower() + "}"; } else { updateBody = "{\"value\":\"" + value.ToLower() + "\"}"; } var content = new StringContent(updateBody, Encoding.UTF8, "application/json"); HttpResponseMessage updateResponse; updateResponse = _httpClient.PutAsync(address, content).Result; if (updateResponse.StatusCode != HttpStatusCode.NoContent) { var ex = new CrmApiException(updateResponse.ReasonPhrase, updateResponse.StatusCode); _logger.LogError(ex, $"[{ex.HttpStatus}] + [{ex.Message}]"); throw ex; } }
public Guid CreateEntity(string entityName, string entityData, bool update = false) { LogInformation($"[{nameof(CreateEntity)}] --> {entityName}/{entityData}"); ApplyAccessToken(); var address = entityName; var content = new StringContent(entityData, Encoding.UTF8, "application/json"); HttpResponseMessage updateResponse; string targetUri; updateResponse = _httpClient.PostAsync(address, content).Result; targetUri = _httpClient.BaseAddress.AbsoluteUri; if (updateResponse.StatusCode != HttpStatusCode.OK && updateResponse.StatusCode != HttpStatusCode.NoContent) { var ex = new CrmApiException(updateResponse.ReasonPhrase, updateResponse.StatusCode); _logger.LogError(ex, $"[{ex.HttpStatus}] + [{ex.Message}]"); throw ex; } IEnumerable <string> headerVals; if (!updateResponse.Headers.TryGetValues("OData-EntityId", out headerVals)) { var ex = new FormatException("Response Entity ID header is empty"); _logger.LogError(ex, $"[FormatException] + [{ex.Message}]"); throw ex; } var idString = new List <string>(headerVals)[0].Replace(targetUri + entityName, ""); return(Guid.Parse(idString)); }
public void CreateBatch(List <BatchData> batchData) { LogInformation($"[{nameof(CreateBatch)}] --> {JsonConvert.SerializeObject(batchData)}"); ApplyAccessToken(); HttpResponseMessage response; // batch setup var batchId = Guid.NewGuid(); var deleteChangeId = Guid.NewGuid(); var patchChangeId = Guid.NewGuid(); var batchUrl = new Uri($"{_httpClient.BaseAddress.AbsoluteUri}$batch"); var batchRequest = new HttpRequestMessage(HttpMethod.Post, batchUrl) { Version = new Version(1, 1) }; var batchContent = new MultipartContent("mixed", "batch_" + batchId); // changeset setup var deleteChange = new MultipartContent("mixed", "changeset_" + deleteChangeId); var patchChange = new MultipartContent("mixed", "changeset_" + patchChangeId); //Add deletes first in their own changeset to avoid issues with upserts in following changesets AddChangeSet(batchData.Where(x => x.Type == BatchTypeEnum.Delete).ToList(), _httpClient, ref deleteChange); AddChangeSet(batchData.Where(x => x.Type != BatchTypeEnum.Delete).ToList(), _httpClient, ref patchChange); //Add headers add changeset level AddHeadersToChangeSets(ref deleteChange); AddHeadersToChangeSets(ref patchChange); // Add the changesets to the batch content batchContent.Add(deleteChange); batchContent.Add(patchChange); // send batch batchRequest.Content = batchContent; response = _httpClient.SendAsync(batchRequest).Result; if (response.StatusCode != HttpStatusCode.OK) { var ex = new CrmApiException(response.ReasonPhrase, response.StatusCode); _logger.LogError(ex, $"[{ex.HttpStatus}] + [{ex.Message}]"); throw ex; } }
public void Associate(Guid entityId1, string entityName1, Guid entityId2, string entityName2, string relationshipKey) { LogInformation($"[{nameof(Associate)}] --> {entityName1}/{entityId1} -> [{relationshipKey}] -> {entityName2}/{entityId2}"); ApplyAccessToken(); HttpResponseMessage resp; var address = $"{entityName1}({entityId1.ToString()})/{relationshipKey}/$ref"; var associated = $"{{\"@odata.id\":\"{_httpClient.BaseAddress}{entityName2}({entityId2})\"}}"; var content = new StringContent(associated, Encoding.UTF8, "application/json"); resp = _httpClient.PostAsync(address, content).Result; if (resp.StatusCode != HttpStatusCode.OK && resp.StatusCode != HttpStatusCode.NoContent) { var ex = new CrmApiException(resp.ReasonPhrase, resp.StatusCode); _logger.LogError(ex, $"[{ex.HttpStatus}] + [{ex.Message}]"); throw ex; } }
public void Delete(string entityName, Guid id) { LogInformation($"[{nameof(Delete)}] --> {entityName}/{id}"); ApplyAccessToken(); HttpResponseMessage response; var method = new HttpMethod("DELETE"); var requestUri = new Uri($"{_httpClient.BaseAddress.AbsoluteUri}{entityName}({id})"); var request = new HttpRequestMessage(method, requestUri); response = _httpClient.SendAsync(request).Result; if (response.StatusCode != HttpStatusCode.NoContent) { var ex = new CrmApiException(response.ReasonPhrase, response.StatusCode); _logger.LogError(ex, $"[{ex.HttpStatus}] + [{ex.Message}]"); throw ex; } }
public JToken RetrieveMultiple(string query) { var frame = new StackFrame(1); var method = frame.GetMethod(); var type = method.DeclaringType; var name = method.Name; LogInformation($"[{nameof(RetrieveMultiple)}] <-- {type.Name}.{name}"); LogInformation($"[{nameof(RetrieveMultiple)}] --> {query}"); ApplyAccessToken(); JToken jretrieveToken = null; HttpResponseMessage retrieveResponse; retrieveResponse = _httpClient.GetAsync(query).Result; if (retrieveResponse.StatusCode != HttpStatusCode.OK && retrieveResponse.StatusCode != HttpStatusCode.NoContent) { var ex = new CrmApiException(retrieveResponse.ReasonPhrase, retrieveResponse.StatusCode); _logger.LogError(ex, $"[{ex.HttpStatus}] + [{ex.Message}]"); _logger.LogError($"[Response] --> {retrieveResponse.Content.ReadAsStringAsync().Result}"); throw ex; } var jretrieveJObject = JObject.Parse(retrieveResponse.Content.ReadAsStringAsync().Result); if (jretrieveJObject == null) { return(jretrieveToken); } jretrieveToken = jretrieveJObject["value"]; LogInformation($" [{retrieveResponse.StatusCode}] --> {jretrieveToken.ToString()}"); return(jretrieveToken); }
public JToken RetrieveMultiple(string query, out int?count) { LogInformation($"[{nameof(RetrieveMultiple)}] --> {query}"); ApplyAccessToken(); JToken jretrieveToken = null; count = null; HttpResponseMessage retrieveResponse; retrieveResponse = _httpClient.GetAsync(query).Result; if (retrieveResponse.StatusCode != HttpStatusCode.OK && retrieveResponse.StatusCode != HttpStatusCode.NoContent) { var ex = new CrmApiException(retrieveResponse.ReasonPhrase, retrieveResponse.StatusCode); _logger.LogError(ex, $"[{ex.HttpStatus}] + [{ex.Message}]"); throw ex; } var jretrieveJObject = JObject.Parse(retrieveResponse.Content.ReadAsStringAsync().Result); if (jretrieveJObject == null) { return(jretrieveToken); } jretrieveToken = jretrieveJObject["value"]; if (jretrieveJObject["@odata.count"] != null) { count = int.Parse(jretrieveJObject["@odata.count"].ToString()); } LogInformation($" [{retrieveResponse.StatusCode}] --> {jretrieveToken.ToString()}"); return(jretrieveToken); }