Not only FakeXrmEasy can help you develop and test applications for the Dataverse / Dynamics CRM that rely on data, but also on metadata.
Some SDK messages, like RetrieveEntityRequest, interact with Entity Metadata in the Dataverse. These are messages that are used to know information about the Entity metadata, that is, information about which fields it has, of which type, and formatting, relationships, and so on.
FakeXrmEasy also contains an In-Memory store that contains information Entity Metadata should you need to use it.
If you need to build and test applications that use these kind of messages, you’ll need to initialize Entity Metadata as well with FakeXrmEasy.
The main issue with entity metadata is that most attributes are readonly and/or have sealed properties. This makes it difficult to setup and/or serialize. However, FakeXrmEasy implements a hack to help you setup Entity Metadata information rather easily in the form of EntityMetadata extensions.
Here’s how:
_context.InitializeMetadata(new EntityMetadata() { LogicalName = "contact" });
InitializeMetadata() also accepts a list of EntityMetadata values like the equivalent .Initialize() method for data initialisation.
Use .SetAttribute() to inject custom EntityMetadata attributes. This example injects a dummy string attribute into the contact entity.
using FakeXrmEasy.Extensions;
var contactMetadata = new EntityMetadata()
{
LogicalName = "contact"
};
var injectedAttribute = new StringAttributeMetadata()
{
LogicalName = "injectedAttribute"
};
contactMetadata.SetAttribute(injectedAttribute);
contactMetadata.SetSealedPropertyValue("PrimaryNameAttribute", "Contact");
Use .SetSealedPropertyValue() to inject a specific EntityMetadata property value.
using FakeXrmEasy.Extensions;
var contactMetadata = new EntityMetadata()
{
LogicalName = "contact"
};
contactMetadata.SetSealedPropertyValue("PrimaryNameAttribute", "Contact");
Use .SetAttributeCollection() to inject a list of attributes into one specific EntityMetadata.
var entityMetadata = new EntityMetadata();
var fakeAttribute = new StringAttributeMetadata() { LogicalName = "name" };
var fakeAttribute2 = new StringAttributeMetadata() { LogicalName = "name2" };
entityMetadata.SetAttributeCollection(new List<AttributeMetadata>() { fakeAttribute, fakeAttribute2 });
There are also extension methods to setup custom attribute metadata properties, of course.
Use .SetSealedPropertyValue() to inject a specific AttributeMetadata property value.
using FakeXrmEasy.Extensions;
var fakeAttribute = new StringAttributeMetadata();
fakeAttribute.SetSealedPropertyValue("IsManaged", false);
By using a combination of the extension methods above and .InitializeMetadata(), should be able to build & test any application that relies on Metadata as well.