Skip to main content

Notifications

Dynamics 365 Community / Blogs / Customizing Dynamics 365 / Getting Dynamics CRM entity...

Getting Dynamics CRM entity records into Python

Hey!

Ever wondered how you can read data from your favorite SaaS CRM ? (Dynamics 365 CE) using Python, one of the most powerful programming languages, easy to write, used to process data, ML , you know the gist etc...)

You came to the right place, I will walk you through how you can access those precious entity records from your Dynamics organization using C# (thanks to Sérgio Ribeiro, Sérgio Marques and Susana Carneiro for the insights in the discussion regarding this community post).

First, open the browser you love the most, navigate to  portal.azure.com and create a new App registration:

0_5F00_create_5F00_app_5F00_registration.png

Give a name of your choice to the App Registration, I will call this one "CrmPythonApp".

In Supported account types select Accounts in this organizational directory only (Default Directory only - Single tenant):

1_5F00_creating_5F00_app_5F00_registration.png

Once the application is created in Azure, we can check its details.

For the next step we will need 2 pieces of information you can find in the screenshot below.

  • the Application client ID (blue box) 
  • The Directory Tenant ID (brown box)

So in the browser leave a tab like the screenshot below open or copy this information to your clipboard.

2350.aaa_5F00_artigo_5F00_imagem1.png

Next, open Visual Studio and create a new Python Application, this will be the app that we use to connect to Dynamics 365 to read those wonderful Dataverse entity records.

2.1_5F00_creating_5F00_python_5F00_app_5F00_registration.png

Once the Python Application is created, just copy the C# code below.

(Fill in the red with Dynamics organization URL, yellow with the Application Client Id and the blue with Directory tenant Id.)

2_5F00_code_5F00_after_5F00_adding_5F00_client_5F00_and_5F00_tenant_5F00_id.png

Notice that we are importing the Microsoft Authentication Library (MSAL), this will allow our applications to be easily integrated with Microsoft Identity platform.

The Microsoft Authentication Library for Python enables applications to integrate with the Microsoft identity platform

Now, if you run the Python application...

run_5F00_python_5F00_app.png

you will get one error like the one below:

3_5F00_error_5F00_i_5F00_obtain_5F00_without_5F00_adding_5F00_permissions.png

This happens because we did not provide the App Registration with the permissions to access Dynamics 365.

In order to do that, go back to your browser, open the Azure tab and check:

App Registration > API permissions > Add a permission > Dynamics CRM > add user_impersonation.

4_5F00_adding_5F00_crm_5F00_permissions.png

Besides that, also add the permission User.Read.All as shown on the picture below:

5_5F00_add_5F00_readall_5F00_crm.png

Once these permissions have been added, try to run the App again, this time you should be prompted with the following message requesting permissions:

6_5F00_new_5F00_message_5F00_you_5F00_will_5F00_get_5F00_after_5F00_step_5F00_4_5F00_and_5F00_5.png

Accept them.

After, you are expected to obtain the message below:

7_5F00_new_5F00_error.png

This happens because no Redirect URI/reply address is registered for the application!

A Redirect URI is needed since it is where the Authorization server will send the user once the app has been successfully authorized and granted an authorization code or access token.

The authorization server sends the token to the redirect URI, so it's important to register the correct one. (for more details, please refer to Redirect URI (reply URL) restrictions - Microsoft Entra | Microsoft Docs ).

So in order to fix that, I will show you how you can add a Redirect URI.

To do this in Azure go to Authentication > Add a Platform > Mobile and desktop applications.

7.1_5F00_mobile_5F00_and_5F00_desktop_5F00_applications.png

Check the first checkbox and add as Custom Redirect URI: http://localhost:1234

Note that the HTTP scheme (http://) is only supported for localhost URIs and should be used only during active local application development and testing.

8_5F00_add_5F00_redirect_5F00_url.png

Once that is done, you should now be able to run the Python Application successfully ! And check those entity records (in this case contacts) being successfully retrieved from your Dynamics organization:

data_5F00_in_5F00_Dynamics.png

contacts.png

Voilá, that´s it!

Now you can process the records from your CRM using Python.

Notice that instead of <Dynamics organization url>/api/data/v9.2/contacts?$select=fullname,contactid

You can also write different API queries to retrieve other records from your Dynamics organization.

Hope you find this article helpful, thank you for reading!

Comments

*This post is locked for comments