Use link for “Connection to Dynamics CRM / D365 WebApi v9 from Console Application C#” . This step is needed to get Client ID after registering app in Azure.
Once connection is made you can use GetAsync to retrieve entities records using webapi query strings example : “[Organization URI] api/data/v9.0/new_test?$select=new_testid,new_amount,new_caseid,new_email&$filter=new_test eq ’12’“
Find the sample code below (Register app in azure and get clientId E.t.c Click Here )
———————————————————————————
public partial class test: System.Web.UI.Page
{
//TODO: Uncomment then substitute your correct Dynamics 365 organization service
// address for either CRM Online or on-premise (end with a forward-slash).
// CRM Online
//TODO: For an on-premises deployment, set your organization credentials here. (If
// online or IFD, you can you can disregard or set to null.)
private static string userAccount = null;//”shashank@sablecafex.onmicrosoft.com“; //CRM user account
//TODO: For CRM Online or IFD deployments, substitute your app registration values
// here. (If on-premise, you can disregard or set to null.)
//private static string clientId = “74b31987-2602-4e05-87d0-77339ac53096″;//”992101fe-6c37-4fb2-b461-2f16505438f6”; //e.g. “e5cf0024-a66a-4f16-85ce-99ba97a24bb2”
private static string clientId = “d933d1a6-5624-4f4f-b135-2a60d059c195”; //e.g. “74b31987-2602-4e05-87d0-77339ac53096″;//”992101fe-6c37-4fb2-b461-2f16505438f6”;
public static httpClient()
{
//One message handler for OAuth authentication, and the other for Windows integrated
// authentication. (Assumes that HTTPS protocol only used for CRM Online.)
HttpMessageHandler messageHandler;
{
messageHandler = new OAuthMessageHandler(serviceUrl, clientId, redirectUrl,
new HttpClientHandler());
}
else
{
//Prompt for user account password required for on-premise credentials. (Better
// approach is to use the SecureString class here.)
Console.Write(“Please enter the password for account {0}: “, userAccount);
string password = Console.ReadLine().Trim();
NetworkCredential credentials = new NetworkCredential(userAccount, password, domain);
messageHandler = new HttpClientHandler() { Credentials = credentials };
}
try
{
//Create an HTTP client to send a request message to the CRM Web service.
using (HttpClient httpClient = new HttpClient(messageHandler))
{
//Specify the Web API address of the service and the period of time each request
// has to execute.
httpClient.BaseAddress = new Uri(serviceUrl);
httpClient.Timeout = new TimeSpan(0, 10, 0); //2 minutes
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
//Send the WhoAmI request to the Web API using a GET request.
var response = httpClient.GetAsync(“[Organization URI] api/data/v9.0/new_test?$select=new_testid,new_amount,new_caseid,new_email&$filter=new_test eq ’12’“,
HttpCompletionOption.ResponseHeadersRead).Result;
if (response.IsSuccessStatusCode)
{
//Body will contain JSON result
JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);
}
else
{
throw new Exception(“error”);
}
}
}
catch (Exception ex)
{
DisplayException(ex);
throw;
}
}
/// <summary> Displays exception information to the console. </summary>
/// <param name=”ex“>The exception to output</param>
private static void DisplayException(Exception ex)
{
while (ex.InnerException != null)
{
throw new exception(“t* {0}”, ex.InnerException.Message);
ex = ex.InnerException;
}
}
}
/// <summary>
///Custom HTTP message handler that uses OAuth authentication thru ADAL.
/// </summary>
class OAuthMessageHandler : DelegatingHandler
{
private AuthenticationHeaderValue authHeader;
public OAuthMessageHandler(string serviceUrl, string clientId, string redirectUrl,
HttpMessageHandler innerHandler)
: base(innerHandler)
{
// Obtain the Azure Active Directory Authentication Library (ADAL) authentication context.
// AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(
//new Uri(serviceUrl + “api/data/”)).Result;
//AuthenticationContext authContext = new AuthenticationContext(ap.Authority, false);
//
string authority = “https://login.microsoftonline.com/760ec4a4-2cfc-4bb5-a39d-aedabed7a992/oauth2/authorize”;;
AuthenticationContext authContext = new AuthenticationContext(authority, false);
//Note that an Azure AD access token has finite lifetime, default expiration is 60 minutes.
// without prompt
AuthenticationResult authResult = authContext.AcquireToken(serviceUrl, clientId, credentials);
//// with prompt
//AuthenticationResult authResult = authContext.AcquireToken(serviceUrl, clientId, new Uri(redirectUrl), PromptBehavior.Always);
//getting authority hedders from token
authHeader = new AuthenticationHeaderValue(“Bearer”, authResult.AccessToken);
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
request.Headers.Authorization = authHeader;
return base.SendAsync(request, cancellationToken);
}
}
——————————————–
Hope this was helpfull. thanks
SOURCE : mscrm.com