Showing posts with label #REST. Show all posts
Showing posts with label #REST. Show all posts

October 18, 2022

Dataverse REST Builder available as Browser Extension

Today (18 October 2022) I did a session at WPC 2022 about Dataverse Web API and I announced that Dataverse REST Builder is now available as browser extension for Google Chrome and Microsoft Edge, here the links if you are interested:

Chrome Web Store

Microsoft Edge Add-ons

Big thanks to my friend Natraj Yegnaraman for suggesting me the idea, his "Level up for Dynamics 365/Power Apps" extension is well known to Dynamics 365/Power Apps users.

How it works Dataverse REST Builder Browser Extension? After you click on the extension icon you are prompted to enter the Environment URL in order to get the Access Token:

After you authenticate, Dataverse REST Builder is loaded in the same way as the XrmToolBox, Managed Solution and Dataverse DevTools versions.

How Dataverse REST Builder is able to obtain the Bearer Token in this scenario? It uses the same Application Client ID defined inside the Microsoft documentation in order to work with Postman: Set up a Postman environment

This Client ID allows two callback URLs, https://localhost and https://callbackurl, inside a browser extension we are able to obtain the URL return after the authentication process is completed (the token is one of the parameters), similar of what Postman does (maybe you don't remember but Postman started as a browser extension).

This solved the main problem, running Dataverse REST Builder inside an extension was indeed possible and doesn't require me to register an Azure application that maybe needs to be whitelisted by your organization, it uses a Microsoft Application Client ID.

The other problem was the use of eval() function to execute the generated code (keep in mind that for generating just the code eval() is not used). Browser extensions stop eval() due to security concerns, however with V3 specifications there is a possibility to load a page inside a sandbox (details here) allowing the use of eval(), so you can execute the generated code as well (Xrm.WebApi still works only with the Managed Solution version).

This is also the reason why there isn't a Firefox extension, Firefox extensions still use V2 specifications and there isn't a concept of sandbox. If in the future they allow this approach I think I can release DRB for Firefox too.

The Browser Extension version can be improved (I have already some ideas), let's see what the future brings.

During the next days I will update the other versions to match the version number (the Browser Extension currently shows 1.0.0.40) and publish the code to a separate GitHub repository.

I hope you find this version useful.

January 15, 2022

Dataverse Web API and REST Client VS Code extension

If you use Visual Studio Code and worked with API you probably know the extension called REST Client. It has more than 2 million downloads, the advantages are that you can use it inside Visual Studio Code (meaning you don't need to have or switch to Postman) and it has a file format (.http) to save the requests.

Let me be clear here, I prefer Postman but as I said many times not everybody uses the same tools so let's explore a bit how REST Client extension works.

If you don't consider authentication it's pretty simple, you write your endpoint and you can click to execute the request, something like this:

What if you need to deal with authentication and the query is against the Dataverse Web API endpoint?
REST Client supports different types of authentication, the one we will use to query the Dataverse endpoint is Client Credentials, recently my friend Benedikt Bergmann published a very useful post for Power Automate (link here) but you can use the first part to configure the App Registration and the required app user.
After we get the required details (Client Id, Client Secret and Tenant Id) we are ready to add the authentication to REST Client.
What I am writing here is not discovered by me, I just combined the information found from this Healthcare API Microsoft Docs page and this post by MVP Geert van der Cruijsen.

First of all we create/update the settings.json in order to add the required syntax by REST Client to add environment variables, like this:

{
    "rest-client.environmentVariables": {
        "$shared": {},
        "Dataverse": {
            "url": "https://mydemo.crm.dynamics.com",
            "tenantid": "89d6b4f0-f93c-4d88-800c-ff6acdae523a",
            "clientid": "71aa6fe0-040f-493f-b19d-9f248692bf93",
            "clientsecret": "notsosecret"
        }
    }
}

If you are familiar with REST Client you probably done something similar, in the above example we defined a "Dataverse" environment and we can use the defined variables.

REST Client format allows to save multiple requests in a single .http file and reuse variables filled by previous requests, the idea here is to first execute a request to get the bearer token and use it as authorization for the next requests.

If we use the V2 Endpoint the request would be like this:

### Get Access Token
# @name getAADToken
POST https://login.microsoftonline.com/{{tenantid}}/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&client_id={{clientid}}&client_secret={{clientsecret}}&scope={{url}}/.default

### Extract access token from getAADToken request
@token = {{getAADToken.response.body.access_token}}
  
We are using the variables defined inside the settings.json and store the bearer token inside a variable called token.

After we can add to the same .http file a request to Dataverse Web API using the token variable:
### Get Contacts
GET {{url}}/api/data/v9.1/contacts?$select=contactid,fullname
Authorization: Bearer {{token}}
OData-MaxVersion: 4.0
OData-Version: 4.0
Content-Type: application/json; charset=utf-8
Accept: application/json
Prefer: odata.include-annotations=*
      
In this way you can now execute your queries inside REST Client.
Why I am writing this? because the latest version of Dataverse REST Builder I just published (1.0.0.24) has a new option to export the requests for REST Client.

The first option "Export REST Client Environment" allows you to create a sample settings.json with the variables I described above, the second option "Export as REST Client Collection (.http)" allows you to download the requests inside the collection as .http file including the authentication request (you can also select the V1 or V2 endpoint)

Hope it helps

October 27, 2021

New tool: Dataverse REST Builder

Note: new version has been released, read more here or download from GitHub

If you created Web API queries against CRM/Dynamics 365/Dataverse probably you are familiar with CRM REST Builder. The amazing tool created by Jason Lattimer helped me countless times in the past years and is a tool I still use today.

The ability to generate queries (especially with Xrm.WebApi when it was introduced) comes very handy, and CRM REST Builder has several functionalities but the one most used is probably the Retrieve Multiple one.

In the past years the development of CRM REST Builder has stopped and beside some bugs the other important missing thing is the compatibility with Unified Interface.

I decided to create a new tool to replace CRM REST Builder, and Dataverse REST Builder (DRB) is finally public at this repository:

https://github.com/GuidoPreite/DRB

 It comes with a Managed Solution to install inside your instance and it will popup inside the Apps list:

As I like to test my code offline, there is also a demo mode, so you can test the application before installing the solution: https://guidopreite.github.io/DRB/

The demo data is not complete but enough to understand how the application works and the demo table  called "Custom Table" contains the majority of columns available inside a Dataverse instance.

Main differences between CRM REST Builder and Dataverse REST Builder are:

  • DRB does not support the 2011 Endpoint (so if you still need it you can continue to use CRM REST Builder)
  • DRB allows to create, save and load a collection of requests, useful if you need to store somewhere the Web API queries you created
  • Support for new columns like "Choices" (also known as Multi Select Option Sets) and better support for "Image" and "File" columns
  • Ability to create requests to execute Custom API
  • A specific request to execute a classic Workflow
  • For Retrieve Single, Create, Update, Delete, it generates also the code for Xrm.WebApi.online.execute

DRB is not perfect and probably there are some bugs as well, but the main functionalities are available and I prefer to release it and gather some feedback.

Some may ask why I decided to create a new tool and not update the existing one. Is a valid concern and I thought about it too, in the end I decided to start from scratch. Main reason is that I had already a solid base to deal with dropdowns (as in my previous tool DOSM) and I designed the application with a different approach, the fact that you can save a collection of requests divides the tool in 3 main parts: a json data structure to hold the request, a UI to manipulate the json data structure and a code generation part accepting the json data structure to create the code.

As developers we need this tool? Honestly not too much, CRM REST Builder is still a great and working tool, and Dataverse REST Builder would not exist without it, I just felt that sometimes we need to deal with new column types or new requests and it's nice to have now a tool.

Hope you find Dataverse REST Builder useful and if something is not working you can open an issue inside the GitHub repo!