web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Python Connectivity wi...
Power Apps
Unanswered

Python Connectivity with Microsoft Dataverse (CDS)

(0) ShareShare
ReportReport
Posted on by 2

Hi team,

 

I'm trying to implement data analysis with python and i need to connect to my tables on CSD, how do i do that?

 

Please share any thoughts/suggestions on this. Thank you.

 

I have the same question (0)
  • Ram Prakash Duraisamy Profile Picture
    5,593 Super User 2025 Season 2 on at

    Hello @PauloBI,

     

    I think this can help you the same

     

    https://www.cdata.com/kb/tech/dynamicscrm-odbc-python-linux.rst

     

    Please mark as Answer if it is helpful and provide Kudos

     

    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA

    Blog : https://microsoftcrmtechie.blogspot.com

  • PauloBI Profile Picture
    2 on at

    Thank's for your time,
    This method works, but I would have to buy the CData solution, wouldn't I have some other way to do it?

  • EricRegnier Profile Picture
    8,720 Most Valuable Professional on at

    Hi @PauloBI,

    You can connect with the restful Web API offered by Dataverse. More info:  https://docs.microsoft.com/powerapps/developer/data-platform/webapi/perform-operations-web-api

    Hope this helps!

  • AndyHowes Profile Picture
    15 on at

    I had this problem too, so I developed a Python program that can connect to the Dataverse / CDS API. I wrote a blog explaining how to set up the Azure App that is required, but I've purposefully made it as simple as possible to connect with your existing role. Use the pcd_download_data.py file to download data in a JSON format which you can then use to do analysis.

     

    Blog: https://andyhowes.co/easily-connect-to-dataverse-microsoft-dynamics-with-python/

    Code: https://github.com/YesWeCandrew/PyConnectDataverse

  • Guido Preite Profile Picture
    1,488 Super User 2024 Season 1 on at

    @AndyHowes if you want you can skip the msal part. You just need to obtain a bearer token, for example using Requests will be something like

    import requests
    
    url = "https://login.microsoftonline.com/[TENANT-ID]/oauth2/v2.0/token"
    
    payload = 'grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET'
    headers = {
     'Content-Type': 'application/x-www-form-urlencoded'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)

    (from the response.text you can extract the bearer token, right now I don't remember the right property, should be access_token)
    and after you use the bearer token in your request, for example a WhoAmI

    import requests
    
    url = "https://[yourorg].crm[number].dynamics.com/api/data/v9.2/WhoAmI()"
    
    payload = {}
    headers = {
     'OData-MaxVersion': '4.0',
     'OData-Version': '4.0',
     'Content-Type': 'application/json; charset=utf-8',
     'Accept': 'application/json',
     'Authorization': 'Bearer [TOKEN HERE]'
    }
    
    response = requests.request("GET", url, headers=headers, data=payload)
    
    print(response.text)

    you can use my tool Dataverse REST Builder to create the requests and export it to Postman if you want to convert them to other languages like Python.
    Thanks for your message, is always nice to see Dataverse connecting to other languages beside .NET

  • AndyHowes Profile Picture
    15 on at

    Hi @GuidoPreite thank you for taking the time to give some feedback!

     

    I initially set up the program as you suggest with Client ID and Client Secret to authenticate. You can see it in the initial commit if you're interested. I did use the ADAL client but could easily be changed to be direct using requests.

     

    I decided to switch to interactive log in because:

    1. Getting a token using client secret requires the app to be given a user/app and security role in the Dataverse environment, and interactive logins enables you to avoid this set up, making the process to get started easier.

    2. The permissions are fixed with the client secret approach, so other users using the program would get data relating to the AAD app's role, business unit etc, instead of their own security role and business unit.

    3. I prefer to avoid client secrets given the risk of them being compromised, although the use of http://localhost with my approach isn't perfect either.

     

    The client secret approach is definitely the way to go for apps that need ongoing access to the data, and in those cases having a dedicated user for the program is absolutely the way to go. I'll consider adding in the ability to use either approach. 

     

    Cheers!

     

     

     

  • Guido Preite Profile Picture
    1,488 Super User 2024 Season 1 on at

    for interactive I usually piggyback the applicationid provided by MS for the samples but I understand your approach.

    Just a note regarding point number 2, you can use impersonation (using the header connected to system user or aad) to execute in a specific user's context.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard