Easily connect to Dataverse/Microsoft Dynamics with Python

Discover the power of connecting to Dataverse, Microsoft Dynamics and Power Apps with Python and unlock a wide range of productivity benefits.

In this article, I show you how to use the program I developed to enable easy, authenticated access to your Dynamics environment’s OData API using Python.

Why connect with Python

There are lots of reasons to connect to your Dataverse data with Python.

For example, when building a new Microsoft Dynamics 365 solution or using Dataverse with Power Apps, often organisational data needs to be imported into Dataverse. I’ve needed to do this often as part of my work developing a CMS for Femili PNG.

Unfortunately, the available options like Power Query Dataflows and the Edit data in Excel feature have limitations such as not supporting many-to-many relationships and limited support for multiple choice data types.

To overcome the many-to-many import problems, I developed a Python program to connect to Dataverse using the Web API and bind oData records together in a many-to-many relationships.

From this I developed a wider library of methods that enable seamless interaction with Dataverse in Python. The complete code is available on GitHub.

Connecting to Dataverse with Python

The primary challenge with using Python to connect with Dataverse is that the existing Python library is no longer maintained, and with the depreciation of ADAL there are limited instructions on how to get the oAuth token needed to authenticate yourself.

To address this, my program uses Azure Active Directory interactive logins and the MSAL library to establish an authenticated session with the Dataverse web API. This session can then be leveraged to execute any of the numerous operations support by the API.

The setup process is quick and straightforward, ensuring both convenience and enhanced security by avoiding the use of client keys that can lead to compromise. Additionally, it allows authentication with existing accounts, eliminating the need to add an application user to your environment.

Key uses

  • Securely accessing all functions of the OData Dataverse API OData Dataverse API with an authenticated Python session.
  • Downloading Dynamics 365 data using Python for efficient analysis.
  • Adding many-to-many relationship data to Dataverse.
  • Automating tedious manual processes such as populating option sets and choices.
  • Anything you can think of using the OData API and Python!

Step-by-step guide to connect to Dataverse

Step 1: Create a new Azure Active Directory app

To begin, navigate to the Azure Active Directory portal and create a new app, by clicking Azure Active Directory > App Registrations > + New Registration.

Give your app a name and select the supported account types. In most cases, choosing Accounts in this organizational directory only is recommended. Leave redirect URI blank for now and complete the registration process by clicking Register.

Step 2: Grant app permissions for Dynamics CRM

In the sidebar of the newly created app, select API permissions > + Add a permission.

Locate the Dynamics CRM permission and select it, enabling user_impersonation.

Once this permission has been selected, Grant admin consent for your organisation to enable you to interact with the data in Python using the permissions of the logged in user.

Step 3: Configure authentication

To enable interactive MSAL authentication, you need to add a Redirect URI of http://localhost.

To do this, in the sidebar of the app, select Authentication > + Add a platform > Mobile and desktop applications > Add the url http://localhost

Scroll down to Advanced settings and select Yes to Allow public client flows.

Step 4: Get app information

The app configuration is now complete. In the sidebar, click on Overview in the app sidebar and note the following details:

  • Application (client) ID
  • Directory (tenant) ID

Additionally, retrieve the base URL of your Environment. You can find this in the Power Platform Admin Centre by selecting the environment you want to connect to.

Always use a Development environment for testing, before using the API on your Production environment.

Step 5: Clone the repo

Clone the PyConnectDataverse repository from Github at: https://github.com/YesWeCandrew/PyConnectDataverse

Use pip to install any packages you haven’t installed previously, such as MSAL.

Step 6: Configure your environment JSON

Using the information gathered in step 4, create an environment JSON. Copy the example-env.json file and replace the values with the details of your app and environment.

Always use a Development environment for testing first, before using the API on your Production environment.

Step 7: Use your favourite IDE to interact with Dataverse with Python 

Test the connection by configuring the parameters in the pcd_test_connection.py file. Run the module to ensure a connection is established. A window will appear, prompting you to log in. If you have already logged in, the process will be automated.

You can now utilise the provided pcd Python files or develop your own functions to interact with Dataverse using Python. Remember to update the specified parameters in each file accordingly.

PyConnectDataverse

Enjoy the benefits of Python-based interaction with Dataverse! I keep finding new uses for this program and am keen to keep using it to speed up common Dataverse tasks.

As new uses for this program continue to emerge, I encourage you to contribute by forking the repository and submitting pull requests to have your useful functions added. I am open to additions and suggestions to enhance the repository further.