2017-7-4 | apurvghai | Dynamics CRM SDK | C#
I’ve added this post to showcase the CRUD operations in C# through WebAPI endpoint. You must have working experience in below areas: –
- Program using C#
- Dynamics 356 Online.
- Visual Studio
- Conceptual knowledge of ODATA Operations.
- Using ADAL + OAuth
All the operations below require access token to be passed as a header hence it’s important to know how to use ADAL (Active Directory Azure Authentication Library).
WebAPI (or REST) endpoints are very easy to program as they just require you to know the authentication protocol then it’s all about sent HTTP Requests with respective OData Filters. Each operation requires a unique set of URLs to be passed. Below you see a sample for creating a record. You can find completed repository on GitHub.
/// <summary>
/// This function can create any record type using Json Entity Objects
/// </summary>
/// <param name="entityName"></param>
/// <param name="entityObject"></param>
/// <returns></returns>
public async Task CreateEntityRecords(string entityName, JObject entityObject)
{
using (httpClient = CreateDynHttpClient(AccessToken, entityName))
{
HttpRequestMessage createHttpRequest = new HttpRequestMessage(HttpMethod.Post, BaseOrganizationApiUrl + "/api/data/v8.1/" + entityName);
createHttpRequest.Content = new StringContent(entityObject.ToString(), Encoding.UTF8, "application/json");
var response = await httpClient.SendAsync(createHttpRequest);
response.EnsureSuccessStatusCode();
if (response.StatusCode == HttpStatusCode.NoContent)
{
// Do something with response. Example get content:
Console.WriteLine("The record was created successfully.");
Console.ReadKey();
}
}
}
Happy WebAPI’ing :)
Cheers,