-2

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".

uba2012
  • 373
  • 1
  • 3
  • 14
  • Use the `Microsoft.CrmSdk` packages. You will need your discovery address, unique organization name, username, password and domain. You can then serialise the `Entity` objects to JSON with the `Newtonsoft.Json` package – p3tch Jul 13 '18 at 14:22
  • Do you have any example of code using that SDK? – uba2012 Jul 13 '18 at 14:32
  • Yes but it's an entire project in itself – p3tch Jul 13 '18 at 16:01

1 Answers1

1

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

Nicknow
  • 7,154
  • 3
  • 22
  • 38
  • Thank!. I do not have a redirectURl, I only have the resource. Is the redirecturl needed ? – uba2012 Jul 13 '18 at 20:40
  • See what they did with the same, just make one up - I usually use a domain I own with a non-existent page (just to avoid any problem.) – Nicknow Jul 14 '18 at 16:48
  • Have u used this before ? I keep getting an error that I cannot call authContext.AcquireToken without using the async. I have a very simple console application currently. – uba2012 Jul 16 '18 at 08:05
  • Make sure you are using ADAL.NET v2 (not v3.) Let me know if that does not solve your issue. – Nicknow Jul 16 '18 at 14:57
  • Ok. I will download the v2 dll. Thanks. – uba2012 Jul 16 '18 at 18:14
  • Hi @Nicknow: I tried the v2 dll and now the authContextToken works.But I get an error saying: "The request body must contain the following parameter: Client_secret or client_assertion". Any idea why this comes up? – uba2012 Jul 17 '18 at 07:35
  • @uba2012 - What was the solution to your **Client_secret or client_assertion** issue? It might be helpful to someone who lands on this question in the future. – Nicknow Jul 17 '18 at 19:18