This is the code using the Microsoft example to authenticate against CRM:
public class CrmConnector
{
private const string ApiVersion = "v8.2";
public static HttpClient Client { get; set; }
public CrmConnector(FileConfiguration config)
{
if (Client == null)
{
Task.WaitAll(Task.Run(async () => await ConnectToCRM(config)));
}
}
protected virtual async Task ConnectToCRM(Configuration config)
{
Authentication auth = new Authentication(config);
Client = new HttpClient(auth.ClientHandler, true);
Client.BaseAddress = new Uri($"{config.ServiceUrl}api/data/{ApiVersion}/");
Client.Timeout = new TimeSpan(0, 2, 0);
Client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
Client.DefaultRequestHeaders.Add("OData-Version", "4.0");
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
}
To pass the config file, will look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings>
<clear />
<add name="default" connectionString="Url=ORG; Username=USER; Password=PASS; Domain=DOMAIN" />
<add name="CrmOnline" connectionString="Url=https://mydomain.crm.dynamics.com/; Username=someone@mydomain.onmicrosoft.com; Password=password" />
</connectionStrings>
<appSettings>
<add key="ClientId" value="CLIENTID" />
<add key="AlternateConfig" value="C:\Temp\crmsample.exe.config"/>
</appSettings>
</configuration>
You can get your clientId from the developer resources in your organization, redirectUrl is optional and you can use the object like this:
CrmConnector CrmConnector = new CrmConnector(new FileConfiguration(null));
And to make the request:
public const string OdataAnnotationAll = "odata.include-annotations=*";
protected virtual async Task<HttpResponseMessage> RequestCRMAsync(HttpMethod method, string query, string pag, bool annotations)
{
HttpRequestMessage request = new HttpRequestMessage(method, query);
request.Headers.Add("Prefer", "odata.maxpagesize=" + pag);
if (annotations)
{
request.Headers.Add("Prefer", OdataAnnotationAll);
}
return await CrmConnector.Client.SendAsync(request);
}
To read the response:
using Newtonsoft.Json;
public virtual async Task<JObject> RequestCRM(HttpMethod method, string query, string pag, bool annotations)
{
JObject responseObject = new JObject();
HttpResponseMessage response = await RequestCRMAsync(method, query, pag, annotations);
if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.NoContent)
{
responseObject = JsonConvert.DeserializeObject<JObject>(response.Content.ReadAsStringAsync().Result);
}
else
{
throw new CrmHttpResponseException(response.Content);
}
return responseObject;
}
If you are making a create or update you have to send the params in the body:
JObject data = new JObject();
data.Add("firstname", "Sxntk");
data.Add("lastname", "IG");
data.Add("annualincome", "1000000000");
request.Content = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
And if it is a create or update, the guid will come in this header:
protected virtual string GetGuidFromResponse(HttpResponseMessage response)
{
string RegexGuid = @"(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}";
// Get the url -> http://~/entity(guid)
var urlHeader = response.Headers.FirstOrDefault(x => x.Key == "OData-EntityId");
//Type of this is KeyPairValue
if (urlHeader.Value == null)
{
return null;
}
// Get the guid form url
return Regex.Matches(urlHeader.Value.FirstOrDefault(), RegexGuid)?[0].Value;
}
Update http method is PATCH not POST.
The clases from microsoft helpers are Authentication, Configuration, Exceptions and CrmConnector modified.