Search code examples
c#odatadynamics-crm-online

Getting data from Dynamics CRM 365 in json


I need to get data from Dynamics CRM 365 Online. Anyone tried this before ?

I need to know what kind of information (clientid, clientsecret) I need, to connect through c sharp and save data (JSON) into a for example a flatfile.

edit: use ADAL.Net v2 If you need to use the non async method. Remember to put the Token in the request header under "Authorization".


Solution

  • You need to use OAuth to authenticate to Dynamics 365 Online from your C# code.

    // TODO Substitute your correct CRM root service address,   
    string resource = "https://mydomain.crm.dynamics.com";  
    
    // 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 = "e5cf0024-a66a-4f16-85ce-99ba97a24bb2";  
    string redirectUrl = "http://localhost/SdkSample";  
    
    // Authenticate the registered application with Azure Active Directory.  
    AuthenticationContext authContext =   
        new AuthenticationContext("https://login.windows.net/common", false);  
    AuthenticationResult result = authContext.AcquireToken(resource, clientId, new Uri(redirectUrl));  
    

    You can then use the AuthenticationResult to make HTTP requests with HttpClient:

    using (HttpClient httpClient = new HttpClient())  
    {  
        httpClient.Timeout = new TimeSpan(0, 2, 0);  // 2 minutes  
        httpClient.DefaultRequestHeaders.Authorization =   
            new AuthenticationHeaderValue("Bearer", result.AccessToken); 
    //TODO Implement your WebApi calls
    }
    

    These code samples and additional details, including how to register an application with Azure AD, are in this link: https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/connect-customer-engagement-web-services-using-oauth