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:
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):
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.
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.
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.)
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...
Besides that, also add the permission User.Read.All as shown on the picture below:
Once these permissions have been added, try to run the App again, this time you should be prompted with the following message requesting permissions:
Accept them.
After, you are expected to obtain the message below:
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.
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.
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:
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!
*This post is locked for comments