Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to Transact data from my API to Dynamic 365 API

Posted on by Microsoft Employee

My requirement is to Sync my database record to Dynamic 365 and also to authenticate the user.

I am trying with this code but it is not helping me out, Please help me to connect and resolve it

string api = "ujwl.api.crm8.dynamics.com/.../v9.0";

AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(api)).Result;

var creds = new ClientCredential("xxx", "xxx");


AuthenticationContext authContext = new AuthenticationContext(ap.Authority);

var token = authContext.AcquireTokenAsync(ap.Resource, creds).Result.AccessToken;

using (HttpClient httpClient = new HttpClient())
{
httpClient.Timeout = new TimeSpan(0, 2, 0);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

HttpResponseMessage response = await httpClient.GetAsync("ujwl.api.crm8.dynamics.com/.../accounts$top=2");
}

I am not able to authenticate user nor getting the data from the server.
Is this approach correct or I need to go with the different approach.

*This post is locked for comments

  • Suggested answer
    Michel van den Brink Profile Picture
    Michel van den Brink 4,697 on at
    RE: How to Transact data from my API to Dynamic 365 API

    Hello,

    The code snippet you are using will unfortunately not work, as is, it is not meant to authenticate with with an application id and secret, not a user's username and password.

    You would need to create an application registration in Azure authenticate with OAuth 2.0

    This article explains how that works:

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/connect-customer-engagement-web-services-using-oauth

      

    May I suggest though that, for simplicy, you take a look at the CrmServiceClient class from the XrmTooling library provided my Microsoft. It makes connecting to Dynamics 365 (CRM) Online much easier and is a great wrapper around the Web API, taking care of most of the heavy lifting and allowing you to focus on your code, rather than creating API calls

    How to connect with CrmServiceClient:

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/xrm-tooling/use-crmserviceclient-constructors-connect

    Quick start sample for CrmServiceClient:

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/xrm-tooling/sample-quick-start-xrm-tooling-api 

    Your code sample, translated to using the CrmServiceClient would look like this:

    
    
    var client = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient("username@companyname",
                        CrmServiceClient.MakeSecureString("password"),
                        "crm8", // region
                        "ujwl", // organizationname
                        useUniqueInstance: true,
                        useSsl: true,
                        isOffice365: true);
    
    Microsoft.Xrm.Sdk.EntityCollection accounts = client.RetrieveMultiple(new Microsoft.Xrm.Sdk.Query.QueryExpression
    {
        EntityName = "account",
        TopCount = 2
    });

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans