{Solved}–Invalid Argument error while showing Virtual entity using Custom Virtual Entity Data Provider on Unified interface

Virtual entities are there for pretty long time now. And why this done and dusted topic? Sometimes the greatest of surprises come from the simplest of stuffs and this was one such scenario.
So here was my customer IT team who had developed a pretty good custom virtual entity data provider to show up virtual entities in CRM. And they had done it a year back and it was working perfectly fine till they decided to make the switch to Unified interface. While the virtual entity is showing up just fine in classic UI, the Unified interface is throwing an error – “Invalid Argument”
To be honest, my first thought was there must have been some changes made to the code but again if that is the case, how come it is still working in Unified Interface. Searched the heck out of Google but nothing much on the Unified interface. So I decided to try something very simple of my own.
I created a custom Virtual entity data provider and wrote a very simple RetrieveMultiple plugin. No dynamic data and all. Just some adhoc records being returned.

public class RetrieveMultiplePlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
             var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            context.OutputParameters["BusinessEntityCollection"] = this.GetEntities();
        }
        public EntityCollection GetEntities()
         {
            var entCollection = new EntityCollection();
           
            for(var i=0; i < 5; i++)
            {
                var ent = new Entity("virtual_demoentity");
                ent["virtual_demoentityid"] = Guid.NewGuid();
                ent["virtual_name"] = $"Record {i + 1}";
                entCollection.Entities.AddRange(ent);
            }
            return entCollection;
        }
    }

As simple as above. Can’t believe anything can be simpler than this. Registered the assembly as Custom Data provider in plugin registration tool. Set up the virtual entity in CRM. Went to classic UI and I could see my five hardcoded records right up there. Piece of cake.
image
Not that I can say of when I navigated to the same Virtual entity on Unified interface.
image
And finally from one expert, got hold of a piece of code which was working. Honestly when I started comparing, for first few passes could not even find the reason. Finally one line caught the eye. And I  made the change from

var entCollection = new EntityCollection();

to

var entCollection = new EntityCollection( { EntityName = “virtual_demoentity”} );

Uploaded it now and it ran just fine in Unified interface as well.
image
So remember, if you are getting this error in Unified interface after designing your Virtual entity data provider and the same is working fine in classic UI, make sure you double check that while creating the instance of EntityCollection, you specify the “EntityName” property.
Hope this helps and saves you some hours for this silly one.
Cheers!
Debajit Dutta
(Dynamics MVP)
For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com
Our product offerings:
Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)
CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)
Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)
Multiselect picklist for Dynamics 365 (http://www.xrmforyou.com/multi-select-picklist.html)