/// <summary> /// Retrieve a list of records based on query options /// </summary> /// <typeparam name="ResultType"></typeparam> /// <param name="uri">e.g. accounts</param> /// <param name="QueryOptions">Filter, OrderBy,Select, and other options</param> /// <returns></returns> public async Task <CRMGetListResult <ResultType> > GetList <ResultType>(string uri, CRMGetListOptions QueryOptions = null) { await CheckAuthToken(); string fullUrl = BuildGetUrl(uri, QueryOptions); HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("GET"), fullUrl); FillPreferHeader(request, QueryOptions); var results = await _httpClient.SendAsync(request); EnsureSuccessStatusCode(results); var data = await results.Content.ReadAsStringAsync(); var values = JObject.Parse(data); CRMGetListResult <ResultType> resultList = new CRMGetListResult <ResultType>(); resultList.List = new List <ResultType>(); foreach (var value in values["value"].ToList()) { if (_crmWebAPIConfig.ResolveUnicodeNames) { FormatResultProperties((JObject)value); } resultList.List.Add(value.ToObject <ResultType>()); } var deltaLink = values["@odata.deltaLink"]; if (deltaLink != null) { resultList.TrackChangesLink = deltaLink.ToString(); } var nextLink = values["@odata.nextLink"]; var recordCount = values["@odata.count"]; if (recordCount != null) { resultList.Count = int.Parse(recordCount.ToString()); } while (nextLink != null) { var nextResults = await _httpClient.GetAsync(nextLink.ToString()); EnsureSuccessStatusCode(nextResults); var nextData = await nextResults.Content.ReadAsStringAsync(); var nextValues = JObject.Parse(nextData); foreach (var value in nextValues["value"].ToList()) { resultList.List.Add(value.ToObject <ResultType>()); } nextLink = nextValues["@odata.nextLink"]; } return(resultList); }
/// <summary> /// Retrieve a list of records based on query options /// </summary> /// <param name="uri">e.g. accounts</param> /// <param name="QueryOptions">Filter, OrderBy,Select, and other options</param> /// <returns></returns> public async Task <CRMGetListResult <ExpandoObject> > GetList(string uri, CRMGetListOptions QueryOptions = null) { await CheckAuthToken(); string fullUrl = BuildGetUrl(uri, QueryOptions); HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("GET"), fullUrl); if ((QueryOptions != null) && (QueryOptions.FormattedValues)) { request.Headers.Add("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\""); } var results = await _httpClient.SendAsync(request); EnsureSuccessStatusCode(results); var data = await results.Content.ReadAsStringAsync(); CRMGetListResult <ExpandoObject> resultList = new CRMGetListResult <ExpandoObject>(); resultList.List = new List <ExpandoObject>(); var values = JObject.Parse(data); var valueList = values["value"].ToList(); foreach (var value in valueList) { resultList.List.Add(value.ToObject <ExpandoObject>()); } var nextLink = values["@odata.nextLink"]; var recordCount = values["@odata.count"]; if (recordCount != null) { resultList.Count = int.Parse(recordCount.ToString()); } while (nextLink != null) { var nextResults = await _httpClient.GetAsync(nextLink.ToString()); EnsureSuccessStatusCode(nextResults); var nextData = await nextResults.Content.ReadAsStringAsync(); var nextValues = JObject.Parse(nextData); var nextValueList = nextValues["value"].ToList(); foreach (var nextvalue in nextValueList) { resultList.List.Add(nextvalue.ToObject <ExpandoObject>()); } nextLink = nextValues["@odata.nextLink"]; } return(resultList); }
/// <summary> /// Retrieve a list of records based on query options /// </summary> /// <param name="uri">e.g. accounts</param> /// <param name="QueryOptions">Filter, OrderBy,Select, and other options</param> /// <returns></returns> public async Task <CRMGetListResult <ExpandoObject> > GetList(string uri, CRMGetListOptions QueryOptions = null) { await CheckAuthToken(); string fullUrl = BuildGetUrl(uri, QueryOptions); var results = await _httpClient.GetAsync(fullUrl); results.EnsureSuccessStatusCode(); var data = await results.Content.ReadAsStringAsync(); CRMGetListResult <ExpandoObject> resultList = new CRMGetListResult <ExpandoObject>(); resultList.List = new List <ExpandoObject>(); var values = JObject.Parse(data); var valueList = values["value"].ToList(); foreach (var value in valueList) { resultList.List.Add(value.ToObject <ExpandoObject>()); } var nextLink = values["@odata.nextLink"]; var recordCount = values["@odata.count"]; if (recordCount != null) { resultList.Count = int.Parse(recordCount.ToString()); } while (nextLink != null) { var nextResults = await _httpClient.GetAsync(nextLink.ToString()); nextResults.EnsureSuccessStatusCode(); var nextData = await nextResults.Content.ReadAsStringAsync(); var nextValues = JObject.Parse(nextData); var nextValueList = nextValues["value"].ToList(); foreach (var nextvalue in nextValueList) { resultList.List.Add(nextvalue.ToObject <ExpandoObject>()); } nextLink = nextValues["@odata.nextLink"]; } return(resultList); }
/// <summary> /// Checks if CRM can be accessed /// </summary> /// <returns>True/False, based on CRM connectivity</returns> public async Task <bool> CRMHealthcheck() { try { // Simple query to CRM to make sure connection can get response CRMGetListResult <SupportEntity> list = await api.GetList <SupportEntity>("era_supports", new CRMGetListOptions { Select = new[] { "era_name", "era_supportid" } }); return(list.List.Count > 0); } catch (Exception e) { logger.LogError(e, "Failed to connect to CRM during health check."); return(false); } }