In this blog we discuss the benefits of using the Microsoft Dynamics 365 Marketing Module!
There are many scenarios where you might need to make a connection to Microsoft Dynamics 365 from an outside source whether it be a single page application, a mobile application, or within some other service. In almost all these situations, authenticating to Dynamics 365 can be a bit challenging. That is until now!
Let's face it, nobody wants to deal with messy SOAP handshake protocols or developing/hosting/managing middleware just to establish a connection to Dynamics 365 from your latest mobile app or through new code running in some website backend. With the help of Azure Apps, we can drastically simplify the development of applications that need a connection to Dynamics 365 (or any other Microsoft Services).
You can perform all of the following steps by creating a trial Dynamics 365 account from Microsoft (did you know that you can access Azure AD without putting in your credit card info?) Below are some high level steps to set up an app in Azure, get a token using that info from C# code, and using the token from a simple JS code to access Dynamics 365.
1. Navigate to Azure Active Directory > App registrations > Click + New application registration
2. Now fill in the required fields as shown below and hit Create. Note that the sign-on URL only matters for something like a single page application – otherwise just putting a localhost URL is just fine.
3. Awesome! Now you have successfully created an Azure app. Double click the app and you will see its details as below.
4. Our next step is to give permission to the app to access Dynamics 365. Navigate to Required permissions and click +Add. Then select Dynamics 365 Online API.
5. Make sure to check the Delegated Permissions checkboxes as shown below. Also, click the Grant Permissions button for the changes to take effect.
6. We have one more thing to setup! Navigate to Keys, create a new key, and copy the value ASAP, be sure to save it somewhere (as it will be hidden in future).
7. Navigate to Endpoints in the App registrations list view and copy the OAUTH 2.0 AUTHORIZATION ENDPOINT URL that you will need in the code later.
Now that we have our Azure app set up complete, we'll move on to creating an application user.
1. Once you create the app user, make sure to give it a custom Security role that has the access you want this user to have.
2. Navigate to your Dynamics 365 org > Settings > Security. Change the view to Application Users and then click +New.
3. This will show you a new user form (make sure to change the form to "APPLICATION USER") where you will only need to fill the "Application ID" field. Use the application ID you created from your Azure app in the previous steps.
Now let's look at the C# code that uses this app to retrieve an Authentication token. We are using the sample code that you can get here.
1. You will need to get the client Id, secret key value, resource URL, and OAUTH 2.0 Authorization Endpoint as described in the previous steps to successfully get the token from the C# code below.
2. We have shown the token in Visual Studio's immediate window, but this token string is what your C# app will return. For the demo, we used the console app but this console app can be hosted in something like an Azure function so that it can be called from anywhere and isn't too difficult to retrieve the Dynamics 365 authentication token.
Now that we have the token, let's use it in a simple JS code. We generated a simple rest call snippet using CRM Rest Builder and we're running the JS code inside jsfiddle. As you can see in the screenshot below, we have successfully sent the WhoAmI request to the Dynamics 365 org and retrieved the values as shown in the alert boxes below.
We hope this method will save some of the CRM developers out there some time and effort when dealing with creating custom third party applications that need to talk to Dynamics 365.
For more helpful Dynamics 365 tips and tricks, be sure to subscribe to our blog!
Happy Dynamics 365'ing!