web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Connect C# console with Web Api dynamics 365 CRM online

(0) ShareShare
ReportReport
Posted on by 195

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

I have the same question (0)
  • Suggested answer
    Summer Garg Profile Picture
    585 on at
    RE: Connect C# console with Web Api dynamics 365 CRM online

    Hi,

    Latest Way to connect C# .Net console application with D365 CRM V9.x

    crmhub.blogspot.com/.../Best-latest-way-to-connect-.net-console-application-with-D365-crm-v9.0-in-vs2019.html

  • Suggested answer
    Vipin J Profile Picture
    1,603 on at
    RE: Connect C# console with Web Api dynamics 365 CRM online

    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

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at
    RE: Connect C# console with Web Api dynamics 365 CRM online

    Hi Aldous,

    In order to access WebAPI in C# Console, first, you need to register the application in Azure Active Directory.

    See: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/walkthrough-register-dynamics-365-app-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.

  • Suggested answer
    sandeepstw Profile Picture
    4,601 on at
    RE: Connect C# console with Web Api dynamics 365 CRM online

    Hi,

    You can try this sample also you can use OrganizationService.SVC as an alternative. Please follow this link for sample code.

    docs.microsoft.com/.../web-api-samples-csharp

  • Suggested answer
    Nflannery Profile Picture
    360 on at
    RE: Connect C# console with Web Api dynamics 365 CRM online

    If it's a console app you might be better off using the SDK.

    sacconsulting.blogspot.com/.../connect-to-crm-via-console-application.html

  • Suggested answer
    PranavShroti Profile Picture
    4,510 on at
    RE: Connect C# console with Web Api dynamics 365 CRM online

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#2
Christoph Pock Profile Picture

Christoph Pock 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans