Azure, Power Platform and Everything cloud

In my previous article, I discussed the many ways we can use azure functions to enhance integration with Dataverse. Most Dataverse developers and Dynamics 365 developers use Dataverse SDK (Xrm.SDK on the D365 days before Dataverse) to write code that interacts with Dataverse. That can be one more thing to learn if you are a developer unfamiliar with query languages like fecthXML or Query Expression. In that case, we can use the Dataverse Web API. To Access the Web API, we can use any language. First, authenticate with MSAL. Then using an HTTP library to invoke the Dataverse API

You can find a version of MSAL for your favorite code language here:

LibrarySupported platforms and frameworks
MSAL for AndroidAndroid
MSAL AngularSingle-page apps with Angular and Angular.js frameworks
MSAL for iOS and macOSiOS and macOS
MSAL Go (Preview)Windows, macOS, Linux
MSAL JavaWindows, macOS, Linux
MSAL.jsJavaScript/TypeScript frameworks such as Vue.js, Ember.js, or Durandal.js
MSAL.NET.NET Framework, .NET Core, Xamarin Android, Xamarin iOS, Universal Windows Platform
MSAL NodeWeb apps with Express, desktop apps with Electron, Cross-platform console apps
MSAL PythonWindows, macOS, Linux
MSAL ReactSingle-page apps with React and React-based libraries (Next.js, Gatsby.js)
https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-overview#languages-and-frameworks

For this example, I will use Python. To get started bootstrapping a fresh blank python Azure Function I will use Visual Studio Code with the Azure Function Extensions. If you already have it, “sign in” to Azure through VS Code using Ctrl+Shift+P and select “Azure: Sign in”. Once you sign in, you should see the “+” sign beside “Workspace”. Select “Create function” (even if creating an HTTP Function).

Create a new project:

Then follow the options presented by the Azure function Extension. Select the compiler version and azure function trigger type of your choice. I’m selecting Python, version 3.9, Http Trigger

That will bootstrap this initial code:

Next, we can add MSAL by oprning a new VS Code terminal (Ctrl+Shift+`) and run “pip install msal”

After MSAL is added, you can import it to the code. We will also use “json” to extract the bearer token returned by the MSAL authentication (Check my previous post to create a service principal and an Dataverse Application user for the value in #1 below):

Line #1 below will return a json with the bearer token:

The part #2 extract the token value from the Json

#3 prepares the http request headers.

#4 is a simple Http Request line using POST to create a Dataverse record in the “accounts”

The code above should be enough to create the simplest record in the Dataverse Account entity. It’s just the bear minimum to give an idea of how to use MSAL and the Dataverse Web Api.

The next step is clean it up a little by first moving secrets and settings to local.settings.json

Next, we can fetch a token using MSAL. I have moved the logic that extracts the token into its own function.

Add some logic to handle url GET parameters:

and finally, a body that returns success or error. the only thing left to do there is to also add the Dataverse url to local.settings.json

Click here to clone the code described above.

Leave a Reply

Discover more from Bruno Lucas Azure Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading