We want to query and call Web API actions on the Microsoft Dynamics CRM 2016 Odata endpoint in Python. We want to do it because we can. Also, Python has a rich set of modules for all sorts from machine learning to forecasting- We want to apply this to CRM data.
The options for integration include using an odata library like ODatapy-Client which generates proxy code or static types from the Web API metadata. Using ODatapy-Client takes a fair amount of work considering that the metadata generated by the CRM web api is a lot and complex.
Given however that all we’re doing is issuing http requests you could use something as simple as cURL. Using PowerShell on an on-premise deployment this would look like this:
curl [Organization URI]/api/data/v8.1/accounts?$select=accountnumber,name -Credential domain\username to get the account number and name for all accounts in CRM. Try that to get a feel of the data.
pycurl allows us to set a number options using the
setopt method which covers most complex http requests that we may have.
requests module we have the following:
We employ the
json module to allow decoding and encoding of json strings returned from our http requests. The
requests module allows you to call requests.json which can also encode the response output as json.
requests also has a Session object which allows you to issue http request in the context of one session and leverage connection pooling.
In order to create a record you can setup the
requests object with the appropriate headers. So given you want to create an account you would do something like this:
In the above example we are issuing a POST request hence the call to
requests.post instead of
get as was done previously.It is also possible to call Web-Api functions. In the below example we call the simple WhoAmI function.
You can pretty much do everything that is supported using the Dynamics CRM 2016 Web API. More on the CRM Web API is available on MSDN.