How to Build an ETL App for SharePoint Data in Python with CData



Create ETL applications and real-time data pipelines for SharePoint 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 SharePoint and the petl framework, you can build SharePoint-connected applications and pipelines for extracting, transforming, and loading SharePoint data. This article shows how to connect to SharePoint with the CData Python Connector and use petl and pandas to extract, transform, and load SharePoint data.

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

About SharePoint Data Integration

Accessing and integrating live data from SharePoint has never been easier with CData. Customers rely on CData connectivity to:

  • Access data from a wide range of SharePoint versions, including Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007 and above, and SharePoint Online.
  • Access all of SharePoint thanks to support for Hidden and Lookup columns.
  • Recursively scan folders to create a relational model of all SharePoint data.
  • Use SQL stored procedures to upload and download documents and attachments.

Most customers rely on CData solutions to integrate SharePoint data into their database or data warehouse, while others integrate their SharePoint data with preferred data tools, like Power BI, Tableau, or Excel.

For more information on how customers are solving problems with CData's SharePoint solutions, refer to our blog: Drivers in Focus: Collaboration Tools.


Getting Started


Connecting to SharePoint Data

Connecting to SharePoint 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.

Set the URL property to the base SharePoint site or to a sub-site. This allows you to query any lists and other SharePoint entities defined for the site or sub-site.

The User and Password properties, under the Authentication section, must be set to valid SharePoint user credentials when using SharePoint On-Premise.

If you are connecting to SharePoint Online, set the SharePointEdition to SHAREPOINTONLINE along with the User and Password connection string properties. For more details on connecting to SharePoint Online, see the "Getting Started" chapter of the help documentation

After installing the CData SharePoint Connector, follow the procedure below to install the other required modules and start accessing SharePoint 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 SharePoint 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.sharepoint as mod

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

cnxn = mod.connect("User=myuseraccount;Password=mypassword;Auth Scheme=NTLM;URL=http://sharepointserver/mysite;SharePointEdition=SharePointOnPremise;")

Create a SQL Statement to Query SharePoint

Use SQL to create a statement for querying SharePoint. In this article, we read data from the MyCustomList entity.

sql = "SELECT Name, Revenue FROM MyCustomList WHERE Location = 'Chapel Hill'"

Extract, Transform, and Load the SharePoint Data

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

Loading SharePoint Data into a CSV File

table1 = etl.fromdb(cnxn,sql)

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

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

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

Adding New Rows to SharePoint

table1 = [ ['Name','Revenue'], ['NewName1','NewRevenue1'], ['NewName2','NewRevenue2'], ['NewName3','NewRevenue3'] ]

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

With the CData Python Connector for SharePoint, you can work with SharePoint 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 CData Python Connector for SharePoint to start building Python apps and scripts with connectivity to SharePoint 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.sharepoint as mod

cnxn = mod.connect("User=myuseraccount;Password=mypassword;Auth Scheme=NTLM;URL=http://sharepointserver/mysite;SharePointEdition=SharePointOnPremise;")

sql = "SELECT Name, Revenue FROM MyCustomList WHERE Location = 'Chapel Hill'"

table1 = etl.fromdb(cnxn,sql)

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

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

table3 = [ ['Name','Revenue'], ['NewName1','NewRevenue1'], ['NewName2','NewRevenue2'], ['NewName3','NewRevenue3'] ]

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

Ready to get started?

Download a free trial of the SharePoint Connector to get started:

 Download Now

Learn more:

SharePoint Icon SharePoint Python Connector

Python Connector Libraries for SharePoint Data Connectivity. Integrate SharePoint with popular Python tools like Pandas, SQLAlchemy, Dash & petl.