This is webapi C#
public class EntityRepository : Repository where T : Crmbaseentity
{
public async Task Add(T entity, string EntityName)
{
using (HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain) }))
{
HttpResponseMessage response = await HttpClientExtensions.SendAsJsonAsync(client, HttpMethod.Post, this.CrmHost + EntityName, entity);
if (response.IsSuccessStatusCode)
return true;
else
return false;
}
}
public async Task Get(string EntityGuid, string EntityName)
{
T item = null;
using (var client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain) }))
{
client.BaseAddress = new Uri(this.CrmHost);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
client.DefaultRequestHeaders.Add("OData-Version", "4.0");
HttpResponseMessage response = await client.GetAsync(EntityName + "(" + EntityGuid + ")");
if (response.IsSuccessStatusCode)
{
var Content = await response.Content.ReadAsStringAsync();
T itemres = JsonConvert.DeserializeObject(Content);
item = itemres;
}
else
{
}
}
return item;
}
public async Task> FilterbyContains(string EntityName,string property,string value)
{
List list = new List();
using (var client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain) }))
{
client.BaseAddress = new Uri(this.CrmHost);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
client.DefaultRequestHeaders.Add("OData-Version", "4.0");
HttpResponseMessage response = await client.GetAsync(EntityName + string.Format("?$filter=contains({0},\'{1}\')", property,value));
if (response.IsSuccessStatusCode)
{
var Content = await response.Content.ReadAsStringAsync();
ODataResponse itemres = JsonConvert.DeserializeObject>(Content);
list = this.CollectionFromResponseSet(itemres.Value);
}
else
{
}
}
return list;
}
public async Task> GetAll(string EntityName)
{
List list = new List();
using (var client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain) }))
{
client.BaseAddress = new Uri(this.CrmHost);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
client.DefaultRequestHeaders.Add("OData-Version", "4.0");
HttpResponseMessage retrieveResponse = await client.GetAsync(EntityName);
if (retrieveResponse.IsSuccessStatusCode)
{
var Content = await retrieveResponse.Content.ReadAsStringAsync();
ODataResponse odataresponse = JsonConvert.DeserializeObject>(Content);
list = this.CollectionFromResponseSet(odataresponse.Value);
}
else
{
}
}
return list;
}
public async Task Update(T entity, string EntityName, string EntityId)
{
using (HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain) }))
{
HttpResponseMessage response = await HttpClientExtensions.SendAsJsonAsync(client, new HttpMethod("PATCH"), this.CrmHost + EntityName + "(" + EntityId + ")", entity);
if (response.IsSuccessStatusCode)
return true;
else
return false;
}
}
public async Task Delete(string EntityName, string EntityId)
{
using (HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain) }))
{
HttpResponseMessage response = await client.DeleteAsync(this.CrmHost + EntityName + "(" + EntityId + ")");
if (response.IsSuccessStatusCode)
return true;
else
return false;
}
}
}
Comments
Post a Comment