Ready to get started?

Learn more about the CData Python Connector for Dynamics CRM or download a free trial:

Download Now

Extract, Transform, and Load Dynamics CRM Data in Python

The CData Python Connector for Dynamics CRM enables you to create ETL applications and pipelines for Dynamics CRM data in Python with petl.

The rich ecosystem of Python modules lets you get to work quickly and integrate your systems more effectively. With the CData Python Connector for Dynamics CRM and the petl framework, you can build Dynamics CRM-connected applications and pipelines for extracting, transforming, and loading Dynamics CRM data. This article shows how to connect to Dynamics CRM with the CData Python Connector and use petl and pandas to extract, transform, and load Dynamics CRM data.

With built-in, optimized data processing, the CData Python Connector offers unmatched performance for interacting with live Dynamics CRM data in Python. When you issue complex SQL queries from Dynamics CRM, the driver pushes supported SQL operations, like filters and aggregations, directly to Dynamics CRM and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations).

Connecting to Dynamics CRM Data

Connecting to Dynamics CRM data looks just like connecting to any relational data source. Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function.

The connection string options meet the authentication and connection requirements of different Dynamics CRM instances. To connect to your instance, set the User and Password properties, under the Authentication section, to valid Dynamics CRM user credentials and set the Url to a valid Dynamics CRM server organization root. Additionally, set the CRMVersion property to 'CRM2011+' or 'CRMOnline'. IFD configurations are supported as well; set InternetFacingDeployment to true.

Additionally, you can provide the security token service (STS) or AD FS endpoint in the STSURL property. This value can be retrieved with the GetSTSUrl stored procedure. Office 365 users can connect to the default STS URL by simply setting CRMVersion.

After installing the CData Dynamics CRM Connector, follow the procedure below to install the other required modules and start accessing Dynamics CRM through Python objects.

Install Required Modules

Use the pip utility to install the required modules and frameworks:

pip install petl
pip install pandas

Build an ETL App for Dynamics CRM Data in Python

Once the required modules and frameworks are installed, we are ready to build our ETL app. Code snippets follow, but the full source code is available at the end of the article.

First, be sure to import the modules (including the CData Connector) with the following:

import petl as etl
import pandas as pd
import cdata.dynamicscrm as mod

You can now connect with a connection string. Use the connect function for the CData Dynamics CRM Connector to create a connection for working with Dynamics CRM data.

cnxn = mod.connect("User=myuseraccount;Password=mypassword;URL=https://myOrg.crm.dynamics.com/;CRM Version=CRM Online;")

Create a SQL Statement to Query Dynamics CRM

Use SQL to create a statement for querying Dynamics CRM. In this article, we read data from the Account entity.

sql = "SELECT FirstName, NumberOfEmployees FROM Account WHERE FirstName = 'Bob'"

Extract, Transform, and Load the Dynamics CRM Data

With the query results stored in a DataFrame, we can use petl to extract, transform, and load the Dynamics CRM data. In this example, we extract Dynamics CRM data, sort the data by the NumberOfEmployees column, and load the data into a CSV file.

Loading Dynamics CRM Data into a CSV File

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'NumberOfEmployees')

etl.tocsv(table2,'account_data.csv')

In the following example, we add new rows to the Account table.

Adding New Rows to Dynamics CRM

table1 = [ ['FirstName','NumberOfEmployees'], ['NewFirstName1','NewNumberOfEmployees1'], ['NewFirstName2','NewNumberOfEmployees2'], ['NewFirstName3','NewNumberOfEmployees3'] ]

etl.appenddb(table1, cnxn, 'Account')

With the CData Python Connector for Dynamics CRM, you can work with Dynamics CRM data just like you would with any database, including direct access to data in ETL packages like petl.

Free Trial & More Information

Download a free, 30-day trial of the Dynamics CRM Python Connector to start building Python apps and scripts with connectivity to Dynamics CRM data. Reach out to our Support Team if you have any questions.



Full Source Code


import petl as etl
import pandas as pd
import cdata.dynamicscrm as mod

cnxn = mod.connect("User=myuseraccount;Password=mypassword;URL=https://myOrg.crm.dynamics.com/;CRM Version=CRM Online;")

sql = "SELECT FirstName, NumberOfEmployees FROM Account WHERE FirstName = 'Bob'"

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'NumberOfEmployees')

etl.tocsv(table2,'account_data.csv')

table3 = [ ['FirstName','NumberOfEmployees'], ['NewFirstName1','NewNumberOfEmployees1'], ['NewFirstName2','NewNumberOfEmployees2'], ['NewFirstName3','NewNumberOfEmployees3'] ]

etl.appenddb(table3, cnxn, 'Account')