Search code examples
angularazuretypescriptdynamics-crmadal

Authenticate and query the Dynamics CRM Web API using Angular 2 (TypeScript)


How do you authenticate with and query the Dynamics CRM Web API from within a SPA built using Angular 2 (TypeScript)?

Research so far has suggested that:

  • the Dynamics CRM (version 2016 or 365) instance must be registered as an application in Azure.
  • users can authenticate with Azure-registered applications via Azure AD. Azure has a client authentication library called ADAL written in JavaScript.

My attempts so far have involved cloning various Angular 2 repos built with ADAL, such as this one, and editing their configuration files to point towards my Azure application. These attempts have all resulted in 401 (Unauthorized) cross-domain errors which is detailed in my separate question.

Using the same configuration (e.g. cliendId) in this JavaScript walk-through from Microsoft is successful.


Solution

  • The code sample provided by Fei Xue posted here on GitHub has proved successful in authenticating with a Dynamics CRM application hosted in Azure and querying the Web API.

    Similar code samples I've used have resulted in 401 Unauthorized HTTP errors. Why this sample works and others do not remains unanswered for the moment.