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 :

Connect to Dynamics CRM in Node.js

Yagasoft Profile Picture Yagasoft 238

There are a couple of ways that we can authenticate with a CRM deployment: Active Directory (AD), Claims, and OAuth. Finding a single library that can easily handle all three in Node.js was a bit of a challenge. Installing multiple libraries, with different methods of usage, is too much hassle for such a straightforward purpose, in my opinion.

The research

To connect to CRM using AD, I tried httpntlm library, and it worked right away without any issues. However, i faced a lot of issues with OAuth; mainly caused by outdated guides on the Internet.

Previously, I worked mainly with Google’s APIs when it came to OAuth, so it was new territory for me working with Azure. So the first step I did was to try the well-known ADAL.js library. It worked fine in a web app with a proper front end, but in my case, I wanted to connect server-to-server. Try as I might, it never worked, as expected as nearly all guides assume there is an interface for the user.

Stumbling upon this article by Lucas: link, I went back to the basics by opting to do it by hand using basic Node built-in functions.

The solution

I followed his steps for the most part, but I wanted to simplify things even more by automatically acquiring the tokenendpoint. The endpoint requires the tenant ID, which can be found by accessing https://login.windows.net/{xyz.onmicrosoft.com}/.well-known/openid-configuration, after replacing xyz.onmicrosoft.com with the domain of the users.

I will add the option to specify the tenant ID in the configuration, for when the above causes an issue I didn’t foresee.

Using the knowledge acquired above, I created a Node.js library that can do the intended job, for AD and OAuth at least.

Installation

Run the following command in your app’s CLI:

npm i node-dcrm-service

The following imports cover the exposed types in this module:

1
import { CrmService, CrmResponse, CrmConnectionConfig, CrmO365ConnectionConfig, CrmAdConnectionConfig } from "node-dcrm-service";

Configuration

Common parameters

1
2
3
{
}

AD parameters

To authenticate with AD, in addition to the above parameters, all you need is the username, password, and domain.

1
2
3
4
5
6
{
    username: "testuser@testorg.onmicrosoft.com",
    password: "password",
    domain: "domain"
}

OAuth parameters

To authenticate with OAuth, you will need a few more parameters:

  1. tenant: the Azure tenant
  2. appId: Application ID in Azure Active Directory
  3. clientId: the Client Secret created in the application above
1
2
3
4
5
6
7
8
9
{
    webApiHost: 'testorg.api.crm.dynamics.com',
    tenant: 'testorg.onmicrosoft.com',
    username: "testuser@testorg.onmicrosoft.com",
    password: "password",
    appId: "16cd08d5-b6f1-475e-90a3-d40d83e26bbc",
    clientId: "Ao+cz9J6MNe/tyizLZR5ili3Oth/vBoZzTr5DqS6r+o="
}

Refer to the following page for more detailed steps on how to acquire the above OAuth parameters: link.

Initialisation

The parameters defined above are passed to the right configuration constructor first:

1
2
3
4
// OAuth
const config = new CrmO365ConnectionConfig(parameters);
// AD
const config = new CrmAdConnectionConfig(parameters);

Next, construct and initialise the service:

1
2
const crmService = new CrmService(config);
crmService.initialise();

Make CRM requests

The service supports the following HTTP verbs: GET, POST, PUT, PATCH, and DELETE.

To make requests to CRM:

1
2
crmService.then(v => crmService.get("WhoAmI()")
    .then(r => console.log(r.body.UserId)));

Contribution

If you would like to contribute to the project, please clone this repository: link.

The post Connect to Dynamics CRM in Node.js appeared first on Yagasoft Blog.

Comments

*This post is locked for comments