logo
Documents & Forms
Microsoft 365 & SharePoint Tools
Classic SharePoint Tools
Forms
May 20, 2020

How to retrieve data from Dynamics 365 CRM using Azure AD app and populate fields of public form

Co-Founder at Plumsail

Previously, I’ve already demonstrated how to retrieve data from SharePoint list and Dynamics 365 Business Central. Now, I will show you how to get data from Dynamics 365 CRM and utilize it in a web form designed with Plumsail Forms. Since at the time of writing this article, Microsoft Graph API does not provide methods for retrieving data from Dynamics CRM, we will use its native API and perform operations on behalf of a specific user. You can also configure app only access and you will find a link to the corresponding documentation describing extra steps you need to follow below but here I’ve tried to demonstrate the most straightforward way of getting data without additional configuration of the Dynamics CRM.

1

This my form with the drop-down field populated from Dynamics CRM:

2

Registration of Azure AD app

First, we need to register a new Azure AD app with access to Dynamics CRM API.

  • Open the Admin center of your Microsoft 365 tenant
  • Expand the left side bar and navigate to Azure Active Directory:

3

  • Go to Azure Active Directory → App registration → New registration:

4

  • Give the app an arbitrary name, ex.: DynamicsCrmFunc
  • Leave the Supported account types on the default setting of Accounts in this organizational directory only
  • Click Register
  • Copy the Application (client) ID of the app. We will use it in Azure Function:

5

Now, we need to configure permissions for the app:

  • Navigate to API Permissions and click Add a permission
  • Under the Microsoft APIs tab, select Dynamics CRM
  • Select user_impersonation under Delegated permissions
  • Click Add permissions

Finally, we need to create a client secret for our app to use it in Azure Function:

  • Navigate to Certificate & secrets and click New client secret
  • Enter any description, specify lifetime, click Add
  • Copy the created secret key.

Deployment of Azure Function

You can deploy sample functions for retrieving Contacts and Accounts directly from our GitHub repository. After the deployment, we’ll need specify client ID and client secret of our Azure AD app, Microsoft 365 tenant, and URL of Dynamics CRM in the Function App settings.

6

  • Open the Function App and navigate to Platform featuresContainer settings (Code Deployment):

7

  • In the Source control step, select GitHub and sign into your account, click Continue
  • In the Build provider step, select App Service build service. Click Continue
  • In the Configure step, select your copy of the data-source-functions repository
  • Click Finish

Once the project is built and deployed, you will find the functions in your Function App. Now, we need to configure it by specifying the Azure AD app properties and URL of the Dynamics CRM.

  • Open the Function App and navigate to Configuration:

8

  • Add the following properties to the Application settings list:
Dynamics365.CRM:AzureApp:ClientId
The Application (client) ID of the Azure AD app

Dynamics365.CRM:AzureApp:ClientSecret
The Client secret of the Azure AD app

Dynamics365.CRM:AzureApp:Tenant
Your Microsoft 365 tenant, ex.: contoso.onmicrosoft.com

Dynamics365.CRM:AzureApp:DynamicsUrl
URL of the Dynamics CRM, ex.: https://contoso.crm.dynamics.com

9

Since we configured the Azure AD app with delegated permissions, we need to grant permissions to our app for requesting data from Dynamics CRM on behalf of specific user. In the Function App, you can find the D365-CRM-Authorize function that is designed specifically for providing delegated permissions. You need to call this function, sign in as a user with access to data you want to retrieve, and accept the permissions. Once you approve the request, D365-CRM-Authorize saves access token in cache for using in other functions. Thus, the end-users won’t be asked for permissions. All requests will perform on behalf of a user who provided consent to the app in the D365-CRM-Authorize function.

Alternatively, you can configure app only permissions for your Azure AD app. In this case, you won’t need to provide user consent to the app and perform operations on behalf of that user. Instead, you will be able to specify granular permissions to your app in Dynamics CRM and use just client ID and client secret for app’s authentication. For this, you need to create Application User for Azure AD app in your Dynamics CRM and configure its roles.

Before calling the D365-CRM-Authorize function, we need to add its URL to Redirect URI’s of the Azure AD app:

  • Copy URL to the D365-CRM-Authorize function:

10

  • Open the Azure AD app and navigate to Authentication section
  • Click Add platform, select Web, and insert the function URL into Redirect URIs text box
  • Click Configure

11

  • Insert the function URL into your browser
  • Sign in as a user with permissions to Dynamics 365 CRM
  • Accept the requested permissions:

12

Finally, we need to enable Cross-Origin Resource Sharing (CORS) for our app to allow JavaScript requests from our web form:

  • Open the Function App and navigate to Platform featuresCORS
  • Add “*” to Allow Origins and remove all other values

13

Designing public web form

Now, we’re ready to design a web form that utilizes data from Azure functions. For this article, I created a simple web form for sending messages to persons from the Contacts list of Dynamics 365 CRM.

First, copy the name of the field for using it in JavaScript:

14

Next, switch to JavaScript editor and add the code below. Do not forget to insert the correct field name and URL of your Azure function:

fd.rendered(function() {
    var widget = fd.field('Contacts').widget;
    widget.setDataSource({
            transport: {
                read: '-- URL of D365-CRM-Contacts function --'
            }          
        });
    widget.setOptions({dataTextField: 'fullname', dataValueField: 'emailaddress1'});
});

Please note that I specified different fields for dataTextField and dataValueField. Thus, a user will select contacts by their names while the submission will contain their emails. So, during processing the submission in Microsoft Power Automate or Zapier, we can send the message directly to the selected contacts without extra requests to Dynamics CRM.

This is my final form:

15

Summary

In previous articles, I’ve described how to retrieve data from Dynamics 365 Business Central and SharePoint list. All samples are gathered in a single GitHub repository, please use it as a starting point for your integrations. Feel free to leave your questions in the comments. If you need to connect your public web form to another data source, just let me know in the comments and possibly I will demonstrate it in one of the future posts.

Note: The post has been originally published at Medium:
Have any questions?
Join our community or contact the support team: