Have you ever heard about Virtual Entity? In short, the Virtual Entity is read-only data. We can pull the data from any other data-source to Dynamics CRM to let users view the data. So now we will be experimenting create Virtual Entity and setup the code until we can see it in the CRM. In this blog-post, we will learn just a simple scenario: RetrieveMultiple without filter/sorting. So we can understand better part by part of the code needed.
Create The Plugin
First, we need to set up the RetrieveMultiple plugin code. Here is the code:
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using RestSharp;
namespace Demo.Plugins
{
public class RetrieveTodos
{
public class TodoModel
{
public int Id { get; set; }
public string Title { get; set; }
public bool Completed { get; set; }
}
public class Operation
{
public TodoModel[] Execute()
{
var client = new RestClient("https://jsonplaceholder.typicode.com/todos");
var request = new RestRequest(Method.GET);
var response = client.Execute<List<TodoModel>>(request);
return response.Data.ToArray();
}
}
}
public class TodosRetrieveMultiplePlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var data = new RetrieveTodos.Operation().Execute()
.Select(rw =>
{
var entity = new Entity("new_todo2")
{
Attributes =
{
["new_name"] = rw.Title,
["new_id"] = rw.Id,
["new_completed"] = rw.Completed,
["new_todo2id"] = Guid.NewGuid()
}
};
return entity;
}).ToArray();
var entityCollection = new EntityCollection(data) { MoreRecords = false, PagingCookie = null };
context.OutputParameters["BusinessEntityCollection"] = entityCollection;
}
}
}
In the above code, you can see that we will take data from JSONPlaceholder. We want to see the To-Dos information in the CRM, so we just need to execute the HTTP-GET method to the URL and deserialize the response into the C# model that we already prepare for it (TodoModel). In this sample, I’m using RestSharp to handle the sending and deserializing. So if you need the same scenario in your company. You sure need to merge it. I already talk about how to merge third-party assembly(s) in this blog post.
After you build and merge, you need to register the plugin. Here is the screenshot of the Plugin that I register in my environment:

Setup Data Provider + Data Source
The next step is to Register New Data Provider. But normally you suppose to prepare the entity first. But when I try to create it using the normal way. The Data Source is not shown in the lookup. That’s why I create the entity after creating the Data Provider + Data Source. So here is the step for creating:
In Plugin Registration Tools > Register > Register New Data Provider. Then you will see this screen and put this information:

You need to go first to Data Source Entity > Create New Data Source > Fill in all the information > click on Create button. You need to wait a while because CRM will create that entity for you. Remember, the name of the entity that you provide will be used for the logicalName in the plugin (in my case the entity name is new_todo2).
After successfully creating the entity, you need to fill in again the information for Data Provider. You need to choose the Assembly. Then you need to map the event handler. For this part, we just need to fill in RetrieveMultiple only.

Setup the Entity
The next step is to create more data columns. In the last step, we already create the entity and the primary field (automatically). So in here I add more columns based on To-Do model:

Registering Plugin Step
Last part, we only need to register the plugin step. Based on my testing, we only can register the plugin in the Post Operation. Here is my configuration for the plugin step:

Here is the result when I open Advanced Find on the To-Do entity:

What you think?
Resources for this blog-post:
Hi there
Great Post! However, we found out that there is no need to register step since it’s been done when we map the event handler during data provider creation.
If we register step on plugin, it will be executed twice which will impact the performance.
Anyway, great sharing, though. Cheers!
Regards,
Jonas
LikeLike
Thank you. I’ll update it for sure.
LikeLike