/// <summary> /// retruns list of matching records /// </summary> /// <param name="entitySetName">example: contacts, opportunities, accounts</param> /// <param name="query">select and filter query and optional orderby</param> /// <param name="isFetchXml">define is the query is fetch xml or not</param> /// <see cref="https://msdn.microsoft.com/en-us/library/mt607843.aspx"/> /// <seealso cref="https://msdn.microsoft.com/en-us/library/gg334767.aspx#bkmk_filter"/> /// <seealso cref="https://msdn.microsoft.com/en-us/library/gg334767.aspx#bkmk_filter"/> /// <seealso cref="https://msdn.microsoft.com/en-us/library/gg309638.aspx#bkmk_passParametersToFunctions"/> /// <see cref="https://msdn.microsoft.com/en-us/library/gg328117.aspx"/> /// <example>?$select=firstname,lastname&$filter=contains(fullname,'(sample)')&$orderby=jobtitle asc, annualincome desc</example> /// <returns>entity Collection</returns> async Task <string> ICrmApiService.RetrieveRecords(string entitySetName, string query, bool isFetchXml = false, int?maxpagesize = null) { ThrowIf.ArgumentNull("entityLogicalName can't be null", entitySetName); ThrowIf.ArgumentNull("query can't be blank", query); if (maxpagesize != null) { HttpCrmClient.DefaultRequestHeaders.Remove("Prefer"); HttpCrmClient.DefaultRequestHeaders.Add("Prefer", $"odata.maxpagesize={maxpagesize}"); } if (isFetchXml) { return(await this.GetRecordsUsingFetchXml(entitySetName, query)); } else { string requestUri = $"{ entitySetName}{query}"; HttpResponseMessage response = await HttpCrmClient.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead); if (response.IsSuccessStatusCode) //200 { string entityCollection = await response.Content.ReadAsStringAsync(); return(entityCollection); } else { throw new Exception(string.Format("Failed to retrieve {0}. Reason: {1}.\n Exception: {2}. \n Query {3}", entitySetName, response.ReasonPhrase, await response.Content.ReadAsStringAsync(), query)); } } }
private async Task <string> GetRecordsUsingFetchXml(string entityLogicalName, string fetchXmlQuery) { //Must encode the FetchXML query because it's a part of the request (GET) string . string requestUri = string.Format("{0}s?{1}", entityLogicalName, WebUtility.UrlEncode(fetchXmlQuery)); HttpResponseMessage response = await HttpCrmClient.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead); if (response.IsSuccessStatusCode) //200 { string entityCollection = await response.Content.ReadAsStringAsync(); return(entityCollection); } else { throw new Exception(string.Format("Failed to retrieve {0}. Reason: {1}.\n Exception: {2}", entityLogicalName, response.Content, await response.Content.ReadAsStringAsync())); } }
/// <summary> /// retruns list of matching record with paging when odataNextLink is provided /// </summary> /// <param name="odataNextLink"></param> /// <param name="maxpagesize"></param> /// <returns>list of records with odataNextLink for Next Retrieve </returns> async Task <string> ICrmApiService.RetrieveRecordsByNextLink(string odataNextLink, int maxpagesize) { HttpCrmClient.DefaultRequestHeaders.Remove("Prefer"); HttpCrmClient.DefaultRequestHeaders.Add("Prefer", $"odata.maxpagesize={maxpagesize}"); HttpResponseMessage response = await HttpCrmClient.GetAsync(odataNextLink, HttpCompletionOption.ResponseHeadersRead); if (response.IsSuccessStatusCode) //200 { string entityCollection = await response.Content.ReadAsStringAsync(); return(entityCollection); } else { throw new Exception(string.Format("Failed to retrieve {0}. Reason: {1}.\n Exception: {2}", odataNextLink, response.ReasonPhrase, await response.Content.ReadAsStringAsync())); } }
/// <summary> /// Retieve Record by Id /// </summary> /// <param name="entitySetName">example: contacts, opportunities, accounts</param> /// <example>contacts, opportunities </example> /// <param name="id"></param> /// <param name="queryOptions"></param> /// <returns>Record</returns> async Task <string> ICrmApiService.RetrieveRecord(string entitySetName, Guid id, string queryOptions) { ThrowIf.ArgumentNull("entityLogicalName can't be null", entitySetName); ThrowIf.ArgumentNull("Record Id can't be blank", id); string requestUri = string.Format("{0}{1}({2}){3}", HttpCrmClient.BaseAddress, entitySetName, id, queryOptions); HttpResponseMessage retrieveResponse = await HttpCrmClient.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead); if (retrieveResponse.IsSuccessStatusCode) //200 { return(await retrieveResponse.Content?.ReadAsStringAsync()); } else { throw new Exception(string.Format("Failed to retrieve contact for reason: {0}\n Exception:{1}", retrieveResponse.Content, await retrieveResponse.Content.ReadAsStringAsync())); } }