Hi,
I am trying to connect console application with WebApi. Can u please guide me and don't give microsoft reference page as answers. i already tried that and nothing is happening.
*This post is locked for comments
Hi,
I am trying to connect console application with WebApi. Can u please guide me and don't give microsoft reference page as answers. i already tried that and nothing is happening.
*This post is locked for comments
Hi,
Latest Way to connect C# .Net console application with D365 CRM V9.x
Refer here for generic method to do CRUD operation in Dynamic CRM using C# in Web API
https://vjcity.blogspot.com/2020/05/generic-web-api-methods-for-dynamic-crm.html
Hi Aldous,
In order to access WebAPI in C# Console, first, you need to register the application in Azure Active Directory.
You will get a client ID after this process.
Now use the code below to get the Authorisation Token. Once you get the token, you can retrieve the required records as shown below.
- static string serviceUri = "yourorg.crmx.dynamics.com/";
- static string redirectUrl = "yourorg.api.crmx.dynamics.com/.../";
- public static string GetAuthToken()
- {
- // TODO Substitute your app registration values that can be obtained after you
- // register the app in Active Directory on the Microsoft Azure portal.
- string clientId = "3oi467rf-2336-4039-b82i-7c5b859c7be0"; // Client ID after app registration
- string userName = "xyz@youOrg.onmicrosoft.com";
- string password = "Password";
- UserCredential cred = new UserCredential(userName, password);
- // Authenticate the registered application with Azure Active Directory.
- AuthenticationContext authContext = new AuthenticationContext("login.windows.net/common", false);
- AuthenticationResult result = authContext.AcquireToken(serviceUri, clientId, cred);
- return result.AccessToken;
- }
- public static void RetrieveAccounts(string authToken)
- {
- HttpClient httpClient = null;
- httpClient = new HttpClient();
- //Default Request Headers needed to be added in the HttpClient Object
- httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
- httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
- httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- //Set the Authorization header with the Access Token received specifying the Credentials
- httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
- httpClient.BaseAddress = new Uri(redirectUrl);
- var response = httpClient.GetAsync("accounts?$select=name").Result;
- if (response.IsSuccessStatusCode)
- {
- var accounts = response.Content.ReadAsStringAsync().Result;
- }
- }
Hope this helps.
Hi,
You can try this sample also you can use OrganizationService.SVC as an alternative. Please follow this link for sample code.
If it's a console app you might be better off using the SDK.
sacconsulting.blogspot.com/.../connect-to-crm-via-console-application.html
please let us know what kind of code you are trying, will it be possible to share here.
Where exactly you are stuck and what is the business scenario you are trying to address ...so that we can help you better
I know you might have already checked this link:
community.dynamics.com/.../programming-using-webapi-through-c-in-dynamics-crm-2016
Regards,
Pranav
If found useful, please mark the answer as verified
André Arnaud de Cal...
292,379
Martin Dráb
231,197
nmaenpaa
101,156