I'm trying to get a list of all the Entities in Dynamics with their row count. I use the code below. The problem is that the second call to get the row count per Entity it throws the error Entity xxx is a virtual entity, which is not supported
.
var list = new List<MyEntityData>();
//make a request to get entities
var entityRequest = new RetrieveAllEntitiesRequest()
{
EntityFilters = EntityFilters.Entity,
RetrieveAsIfPublished = true
};
//call the request
var entityResponse = (RetrieveAllEntitiesResponse)Dynamics._service.Execute(entityRequest);
//make a request to get the entity row count
var request = new RetrieveTotalRecordCountRequest
{
EntityNames = entityResponse.EntityMetadata.Where(x => x.CanCreateForms.Value == true)
.Select(x => x.LogicalName).ToArray()
};
//call the request
var response = (RetrieveTotalRecordCountResponse)Dynamics._service.Execute(request); //error here
//loop all the results to get the entity name with the row count
foreach (var item in response.EntityRecordCountCollection)
{
list.Add(new MyEntityData()
{
name = item.Key,
rows = (int)item.Value
});
}
I tried to filter out the Entities from entityResponse
by setting RetrieveAsIfPublished = true
or .Where(x => x.CanCreateForms.Value == true)
, but both don't work, the Virual Entiries are still there.
So does anyone know how to filter out Virtual Entities from the entityResponse
?
UPDTATE
This is the filtering that actually worked for removing Virtual Entities from the list.
entityResponse.EntityMetadata.Where(x => string.IsNullOrEmpty(x.ExternalCollectionName)).Select(y => y.LogicalName)
@Andrew Butenko 's answer is already very good, but I still want to add some content.
After creating a virtual entity, the Data Source can be set to Null in UI. But
External Name and External Collection are only used by virtual entity and they are required, so you can also use these fields.
In fact, TableType(String Property) is a straight way. For normal entity, it is "Standard" and for Virtual Entity it is "Virtual".