How to use Quick find view to search records in DataVerse/ Dynamics 365 programmatically using Web API

Hello everyone and welcome to my blog. In today’s blog I will discuss an interesting topic on how to perform search queries using Quick find view in DataVerse/ Dynamics 365 through WebApi endpoint.

In DataVerse, Quick find view drives the results of searching in the search text box of an entity. If we have a requirement to search a table in DataVerse programmatically, the easiest option would be to leverage the Quick find view.

Through my blog I share interesting tips and discuss on latest releases of Microsoft.NET technologies, Dynamics 365 and Power Platform, SharePoint and Client scripting libraries. Please subscribe to my blog to stay updated.

But how do I do that? Dynamics 365 Web API’s support queries against saved views and personal views. You can find more details here.

Below illustration show how you can query ‘Active Accounts‘ view in DataVerse.

https://<org-url>/api/data/v9.0/accounts?savedQuery=00000000-0000-0000-00aa-000010001002

In the savedQuery parameter you need to pass the Guid of the view

And below is the result of the query.

We can perform similar query for Quick find view. But unfortunately it does not return any result.

https://<org-url>/api/data/v9.0/accounts?savedQuery=2d1187c4-23fe-4bb5-9647-43bb1c6ddbd1

So how do we search against Quick find views. Well, it turned out to be bit more difficult than I expected.

After doing some research, I found there is a request – ExecuteQuickFind in DataVerse SDK. But how to execute this request using Web API.

I didn’t find any official documentation in MS Learn where this request message is documented. Looks like it is there and you can use it but not sure if this is supported. So I shall say to use this with caution.

After searching the heck out of Google, I got this wonderful blog which show how to use the ExecuteQuickFind request using DataVerse .NET SDK. A blog written 8 years back and certainly ahead of it’s time. Just one of the many wonderful blog posts from MVP – Natraj Yegnaraman.

Taking a que from that blog, I understood there are couple of parameters which are important to execute this request. One is SearchText and the other EntityNames. If your requirement is to execute that using SDK, the blog post will be good enough.

The task for me now is to convert this to Web API request. Below is the code to perform this request using Xrm.WebApi methods from client side.

// JavaScript source code
var request = {};
request.SearchText = "Adventure";
request.EntityNames = ["account", "contact"]; //specify the entity logical names. Max upto 10.

request.getMetadata = function () {
   return {
      boundParameter: null,
      parameterTypes: {
         "SearchText": {
            "typeName": "Edm.String",
            "structuralProperty": 1
         },
         "EntityNames": {
            "typeName": "Collection(Edm.String)",
            "structuralProperty":4

         }

      },
      operationType: 0, 
      operationName: "ExecuteQuickFind"
   };
};

Xrm.WebApi.execute(request).then(function (result) {
   result.json().then((response) => {

      for (var e = 0; e < response.Result.length; e++) { //the result array length will depending on the number of entities you have specified in the request.
         let entityResult = response.Result[e];

         for (var d = 0; d < entityResult.Data.length; d++) {
            let dataRow = entityResult.Data[d];

            // dataRow will contain the columns in of your Quick find.
         }
      }

   });
},
   function (error) {
      debugger;
   });

The code is self explanatory and I have put in appropriate comments wherever required. If you are thinking how to execute the same without using Xrm.WebApi classes, the below sample should help you.

Below illustration shall work for all scenarios where you want to leverage Quick find view to search data in DataVerse/ Dynamics 365 from third party applications. This is very helpful for integration scenarios.

      let request = {};
      request.SearchText = "Adventure";
      request.EntityNames = ["account", "contact"];

      let headers = new Headers();
      headers.append('Content-Type', 'application/json;charset=utf-8');
      headers.append("Accept", "application/json");
      headers.append("OData-MaxVersion", "4.0");
      headers.append("OData-Version", "4.0");
      headers.append("Authorization", `Bearer ${<your bearer token>}`);

      let options = {
         method: 'POST',
         headers: headers,
         body: JSON.stringify(request)
      };

      fetch(`${<org-url>}/api/data/v9.2/ExecuteQuickFind`, options)
         .then((response) => {
            debugger;
            response.json().then((result) => 
              {
                  // parse your result here
              })
         })
         .catch(error => console.log(error));

The code is in Javascript but you can replicate this easily in any language.

Not a requirement you would get everyday but certainly worth learning and sharing.

Hope this helped. For similar interesting topics on Microsoft.NET and Power platform, please subscribe to my blog.

Debajit Dutta
Business Solutions MVP


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

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

Continue reading