Virtual Entity in Dynamics 365: 2023 Guide

  • Download Visual Studio

  • Go to extension > install power platform tools

    (or download here)

  • choose Tools > Connect to Dataverse

  • go to view> Power Platform Explorer

  • Expand the target environment's node.

  • Right-click the Plug-in Assemblies node. You will see Install Profiler, this will help you to debug server database

  • Use Power Platform App C#

method 2:

  • Create a C# Library app.

  • Install power app tools extention

  • right Click on the solution name in the right bar and find nuget package manager

    • Install crm assemblies and plugin registration tool
  • once done extended the I plugin extetion to gt the execute method

we will talk about a few things about plugins before creating plugin for virtual entity

  • What is plugin

  • what is a plugin registration tool

  • How to write and register the plugin

  • How to Debug the plugin

  • Retrieve and Retrieve Multiple plugins

Virtual Entity

A virtual entity is a type of table in Microsoft Dataverse that enables the integration of data residing in external systems without replication of data and often without custom coding.

It is a definition of a table in the Dataverse platform without the associated physical tables for records created in the Dataverse database. Instead, when a record is required during runtime, its state is dynamically retrieved from the associated external system.

Virtual entities replace previous client-side and server-side approaches to integrating external data, which required customized code and suffered from numerous limitations, including imperfect integration, data duplication, or extensive commitment of development resources. In addition, for administrators and system customizers, the use of virtual entities dramatically simplifies administration and configuration.

How to fetch or retrieve data using the plugin in a virtual entity:

  • Query Expression

  • FetchXML

  • OData

To fetch data from a virtual entity, you can use the standard query methods available in Dynamics 365, such as QueryExpression, FetchXML, or OData. The query is sent to the virtual entity’s data provider, which retrieves the data from the external data source and returns it to Dynamics 365. The data is then displayed in the user interface as if it were stored in the Dataverse database.

For example, here’s a sample code that uses QueryExpression to retrieve data from a virtual entity named new_virtualentity:

var query = new QueryExpression("new_virtualentity");
query.ColumnSet = new ColumnSet("new_name", "new_description");

var response = service.RetrieveMultiple(query);

foreach (var entity in response.Entities)
{
    Console.WriteLine(entity["new_name"]);
    Console.WriteLine(entity["new_description"]);
}

This code creates a new QueryExpression for the new_virtualentity table and specifies the columns to retrieve using a ColumnSet. The query is then executed using the RetrieveMultiple method of the IOrganizationService interface. The results are returned as an EntityCollection, which can be iterated to access the individual records.

A Sample Retrieve Multiple plugin

ref: Dynamics 365 RetrieveMultiple Plugin - Carl de Souza


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace Carl.Crm.RetrieveMultiple
{
    public class AccountMultiple : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
            serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.OutputParameters.Contains("BusinessEntityCollection"))
            {
                var businessEntityCollection = (EntityCollection)context.OutputParameters["BusinessEntityCollection"];

                foreach (Entity e in businessEntityCollection.Entities)
                {
                      // Add code
                }
            }
            else
            {
                throw new InvalidPluginExecutionException("Error");
            }
        }
    }

Using FetchXML to retrieve records

ref: Use FetchXML to query data (Microsoft Dataverse) - Power Apps | Microsoft Learn

go to advance find and copy the XML of the entity you want to fetch

<fetch version='1.0' mapping='logical' distinct='false'>  
   <entity name='entitymap'>  
      <attribute name='sourceentityname'/>  
      <attribute name='targetentityname'/>  
      <link-entity name='attributemap' alias='attributemap' to='entitymapid' from='entitymapid' link-type='inner'>  
         <attribute name='sourceattributename'/>  
         <attribute name='targetattributename'/>  
      </link-entity>  
   </entity>  
 </fetch>

use XML in file and call it fetch XML or create a function FetchXML or String/Var like this

// Retrieve all accounts owned by the user with read access rights to the accounts and   
// where the last name of the user is not Cannon.   
string fetch2 = @"  
   <fetch mapping='logical'>  
     <entity name='account'>   
        <attribute name='accountid'/>   
        <attribute name='name'/>   
        <link-entity name='systemuser' to='owninguser'>   
           <filter type='and'>   
              <condition attribute='lastname' operator='ne' value='Cannon' />   
           </filter>   
        </link-entity>   
     </entity>   
   </fetch> ";   

EntityCollection result = _serviceProxy.RetrieveMultiple(new FetchExpression(fetch2));
foreach (var c in result.Entities)
{
   System.Console.WriteLine(c.Attributes["name"]);
}