Category Archives: Others

Integrate Python to connect to Dynamics 365/ CDS Web API using Client credentials

Well this is my third post on Python and I am loving the language and it’s simple syntax of doing things.

So I decided to explore on how to connect to Dynamics 365 WebAPI using Python. And I came across this wonderful post – https://alexanderdevelopment.net/post/2016/11/27/dynamics-365-and-python-integration-using-the-web-api/. Highly recommended you go through this one.

While implementing the same, I stumbled upon few areas which I will help to resolve. Also the blog demonstrates with grant_type=password. I will show with grant_type=clientcredentials.

The first thing is obviously register an app with Azure AD and get the client_id and client_secret. And also make sure that the app have permissions to access the CDS environments. If you are unware of these, there are hundreds of blogs to guide you through this. For the brevity of this blog, I am skipping this one.

let’s jump to the code now. I am using Sublime Text 3 editor for this. However you can use any python editor of your choice.

image

If you really look at the code, you can draw a sharp analogy to C#. I am basically importing the JSON and requests library to work with web requests and parsing the json response. The rest of the code is explanatory. In this code I am just writing the code to get the access token. In the next blog I am going to write on how to fetch data using the token. But at the moment, I leave that you.

I build and run the code. But I get the error – “module requests not found”. This is more of C# build error where it says some dll is missing. Just like we need to install the package, so do we need to do the same here as well. But if you are new to python like me, even the simplest of things take time.

To install the requests module, open the command prompt in your machine and go to scripts folder of python installation library. Then use the command “easy_install requests”.

image

And the requests package shall be installed successfully.

Now I build and run and I can see the access token being printed on the console. Delight for starters like me.

image

Hope you found this interesting. You can follow my blog for more posts like this.

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Notes Manager (https://debajmecrm.com/2019/02/28/add-metadata-to-your-notes-and-attachments-in-dynamics-notes-metadata-manager-from-xrmforyou-com/)

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)

How to print special characters like Beta, alpha, Phi while using CSV writer in Python

This is my second blog post and gosh, I am loving it here.

So here it goes. I had a requirement where I had to print the special greek characters like Alpha, beta, omega, Phi etc in excel using CSV module.

Since I am pretty new to the platform, I searched around in the wonderful community of python and realized that I need to open the file with utf-8 encoding for writing the .csv file.

Advertisements

Sample code below where I am printing the beta and phi characters using the unicode values of those characters.

import csv

with open(’emissions.csv’, ‘w’, encoding=”utf-8″, newline=”) as csvfile:
     filewriter = csv.writer(csvfile, delimiter=’\t’,
                             quotechar=’\u01C0′, quoting=csv.QUOTE_MINIMAL)
     filewriter.writerow([”, ‘Natural Gas’, ‘Diesel’, ‘Fuel oil’, ‘Coke’])
     filewriter.writerow([‘NHV (MJ/Kg)’, ‘51.2’, ’42’, ’40’, ‘30.0’])
     filewriter.writerow([‘\u03B2’, ‘0.76’, ‘0.87’, ‘0.85’, ‘0.74’]) – unicode for beta
     filewriter.writerow([‘\u03D5’, ‘3.5’, ‘3.7’, ‘3.7’, ‘5.3’]) – unicode for Phi
     filewriter.writerow([‘CER’, ‘0.052’, ‘0.077’, ‘0.079’, ‘0.131’])
     filewriter.writerow([‘COST($/KG)’, ‘0.34’, ‘0.33’, ‘0.28’, ‘0.04’])

Unfortunately when I ran the code, the excel file didn’t have the correct characters printed. As you can see from the below screenshot, the characters didn’t get printed at all.

Advertisements
image

I am quite proficient in c# having worked on the platform for the last 12 years. Using the same logic with which I would have done there, instead of using the utf-8 encoding, I used the utf-32 encoding this time. The characters printed fine. However the issue now happened is all the column values were showing up in a single excel column instead of separate columns in excel.

image

I was close and on seeing this I realized, this may be due to delimiter I am using for my .csv file. So instead of using comma as the delimiter, I started using tab (\t) as the delimiter.

image

And now it worked just fine.

Advertisements

Hope this helps!

Debajit Dutta

(Microsoft MVP & MCT)

Blank row getting inserted between each row of excel while using CSV writer in Python

I have recently started working on phyton and I just love it. And now I wonder why I didn’t get an opportunity to start earlier.

So here it is. I was trying to write a CSV file. Below is the sample code for the same.

Advertisements

import csv

with open(’emissions.csv’, ‘w’, encoding=”utf-32″) as csvfile:
     filewriter = csv.writer(csvfile, delimiter=’\t’,
                             quotechar=’\u01C0′, quoting=csv.QUOTE_MINIMAL)
     filewriter.writerow([”, ‘Natural Gas’, ‘Diesel’, ‘Fuel oil’, ‘Coke’])
     filewriter.writerow([‘NHV (MJ/Kg)’, ‘51.2’, ’42’, ’40’, ‘30.0’])
     filewriter.writerow([‘\u03B2’, ‘0.76’, ‘0.87’, ‘0.85’, ‘0.74’])
     filewriter.writerow([‘\u03D5’, ‘3.5’, ‘3.7’, ‘3.7’, ‘5.3’])
     filewriter.writerow([‘CER’, ‘0.052’, ‘0.077’, ‘0.079’, ‘0.131’])
     filewriter.writerow([‘COST($/KG)’, ‘0.34’, ‘0.33’, ‘0.28’, ‘0.04’])
   

While everything was working just fine, the emissions.csv file was having a blank row between each record.

image

The problem is, when I am opening the .csv file, I am opening it using ‘w’ option which basically means I am opening it in text mode. Hence “\r\n” is getting printed after each row. Ideally i should always open the .csv file in binary mode using ‘wb’ option.

Advertisements

But in case you are opening it with ‘w’, use the newline=’’ in your open construct

import csv

with open(’emissions.csv’, ‘w’, encoding=”utf-32″, newline=”) as csvfile:
     filewriter = csv.writer(csvfile, delimiter=’\t’,
                             quotechar=’\u01C0′, quoting=csv.QUOTE_MINIMAL)
     filewriter.writerow([”, ‘Natural Gas’, ‘Diesel’, ‘Fuel oil’, ‘Coke’])
     filewriter.writerow([‘NHV (MJ/Kg)’, ‘51.2’, ’42’, ’40’, ‘30.0’])
     filewriter.writerow([‘\u03B2’, ‘0.76’, ‘0.87’, ‘0.85’, ‘0.74’])
     filewriter.writerow([‘\u03D5’, ‘3.5’, ‘3.7’, ‘3.7’, ‘5.3’])
     filewriter.writerow([‘CER’, ‘0.052’, ‘0.077’, ‘0.079’, ‘0.131’])
     filewriter.writerow([‘COST($/KG)’, ‘0.34’, ‘0.33’, ‘0.28’, ‘0.04’])

    

Once you do this, you shall see your .csv file being created without the blank rows.

Advertisements

Hope this helps!

Debajit Dutta

(Microsoft MVP & MCT)