Learn your building blocks

In my opinion, the biggest responsibility for a software developer is break down black boxes and study the unknown. You will do this every day as you learn how something

Latest Post Learn your building blocks by Magnus Gether Sørensen

In my opinion, the biggest responsibility for a software developer is break down black boxes and study the unknown. You will do this every day as you learn how something works. In this blog I will give a practical demonstration of how tools and applications can be built from basic API calls. I hope that this will inspire you to create something amazing of your own!

The first step of this journey is to find a place to do your tinkering. This should be in an environment where you are comfortable. The good thing is Microsoft Dataverse supports a ton of ways of interacting with it, including an API that you are hopefully already familiar with to some degree. Depending on your language of choice there are several environments I can recommend.

The purpose for your environment should be to reduce the time from idea to result. Rapid prototyping is key. Your goal is to create something that can be used by you to solve your task. After you have your prototype you can take your code somewhere else to make a tool or application out of it.

The next step would be to familiarize yourself with the actions available to you in the API. As of writing there are 366 requests available. You probably know a few, others you would know from the buttons that you have used from the UI. The purpose of learning the actual requests is that by knowing your building blocks you will know how to create skyscrapers. But beware, once you know everything you can do, you will no longer be amazed by new tools or applications written on Microsoft Dataverse, because you will instinctly know how they are probably made.

The final part is to use these building blocks. As a practical exercise consider a newer tool for the XrmToolBox - Fast Record Counter. The purpose of the tool is to count the number of records for selected entities.

The first step in creating a tool like this would be to count a single entity. Luckily we know our API by heart and know that there exists a request for this - RetrieveTotalRecordCount. This request takes an array of entity names as strings and returns the count for those records.


var req = new RetrieveTotalRecordCountRequest()
{
    EntityNames = new string[] { 
        "account" 
    }
};
var resp = service.Execute(req) as RetrieveTotalRecordCountResponse;
resp.EntityRecordCountCollection;

So this request on its own would probably be enough to finish this tool. However, the creater is clever and knows that you can make things faster.

Following the studies of Mark Carrington it is known how to make multiple calls faster. The fastest way is to utilize multi-threading and the request ExecuteMultiple, which the tool does in fact do to count records as fast as possible.


var requestsCollection = new OrganizationRequestCollection()
{
    new RetrieveTotalRecordCountRequest(),
    new RetrieveTotalRecordCountRequest(),
    new RetrieveTotalRecordCountRequest(),
    ...
};
                
Parallel.ForEach(requestsCollection,options,chunk =>
    var batchReq = new ExecuteMultipleRequest()
    {
    Requests = new OrganizationRequestCollection().AddRange(chunk),
    Settings = new ExecuteMultipleSettings
        {
            ContinueOnError = true,
            ReturnResponses = true
        }
    };
    var resp = servicestage.Execute(batchReq) as ExecuteMultipleResponse
    ...
)

This is a sparse example. For a detailed and real example from the actual tool go look at the code. This tool used two different building blocks, a request to get the count and a method of making it go fast.

What I hope you take away from this that even the most complex tools or applications out there can be broken down into simple request calls.
Do you need information about an entity? Use the RetrieveEntityRequest.
Want to know something about security roles, workflows or plugins? They are real data in the database, use a RetrieveMultipleRequest. The last step is just to figure out what problem you want to solve!

Magnus Gether Sørensen

Published 9 months ago