Extending CrmSvcUtil.exe


04.23.19 Posted in Technology by

One of the projects that I worked on was to create an internal web form application that is bound to Microsoft Dynamics 365 for Customer Engagement. The bind is with early bound classes that are generated by the Code Generation tool. During development, these classes are regenerated several times which can contain unnecessary entities that bloat the Visual Studio project. To solve this problem, I extended the Code Generation tool to filter out unnecessary entities.

What is Dynamics 365 for Customer Engagement apps?

Dynamics 365 for Customer Engagement  is a cloud-based Customer Relationship Management business solution that provides out-of-the-box features that help drive sales, productivity, and efficient organizational management. […]

Code Generation Tool

Code Generation Tool is an executable file (CrmSvcUtil.exe) that generates early-bound .NET Framework classes that represent entity data model used by Dynamics 365 for Customer Engagement apps. […]

Early Bound Class

Early bound classes are used to access business data by applications that use Dynamics 365 for Customer Engagement. Each class contain entities which hold attributes and relationships.

Limitation

One limitation of the code generation tool is that it generates all entities by default which result to a bloated Visual Studio project. This potentially slows down compiling or debugging during development.

Solution

A solution to this problem is extending the Code Generation Tool by implementing the interface ICodeWriterFilterService. Sebastian Holager’s CRMSvcUtilExtensions solution on Github provides two filters:

  1. VanillaFilter – Filters out any custom entity, attribute and relationships
  2. SolutionFilter – Generates only entities that is incuded in the solution that is specified

I added the class EntityFilter that generates entities which correspond to the entities listed in an XML file. Please see the source code below.

For the full codebase, go to https://github.com/ezraundag/crmsvcutilextensions

....
public class EntityFilter : ICodeWriterFilterService
{
    private readonly ICodeWriterFilterService defaultService;
    private const string filterXML = "filter.xml";
    private List<string> _filterEntities = new List<string>();

    public EntityFilter(ICodeWriterFilterService defaultService)
    {
        this.defaultService = defaultService;
        this.LoadFilter();
    }

    private void LoadFilter()
    {
         string filterXml = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\" + filterXML;
         XmlDocument doc = new XmlDocument();
         doc.Load(filterXml);
         XmlNodeList entities = doc.GetElementsByTagName("entity");
        foreach (XmlNode i in entities)
        {
            _filterEntities.Add(i.InnerText);
        }
     }

    private bool IncludeEntity(string entityname)
    {
        return (_filterEntities.Contains(entityname));
    }

    public bool GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
    {

        if (!this.IncludeEntity(entityMetadata.LogicalName))
        {
             return false;
        }
        return defaultService.GenerateEntity(entityMetadata, services);

    }

    public bool GenerateAttribute(AttributeMetadata attributeMetadata, IServiceProvider services)
    {

        if (!this.IncludeEntity(attributeMetadata.EntityLogicalName))
        {
            return false;
        }
        return defaultService.GenerateAttribute(attributeMetadata, services);
    }

....

Usage

  1. List the needed entities in filter.xml.
  2. Place the xml file in the same directory as CrmSvcUtil.exe.
  3. Run CrmSvcUtil.exe.

CrmSvcUtil.exe /codewriterfilter:"CRMSvcUtilExtensions.EntityFilter,CRMSvcUtilExtensions" /url:... /out:D:\Entities.cs /username:... /password:... /namespace:...

Tips

If you are running the CodeGeneration tool behind a proxy, edit CrmSvcUtil.exe.config and add the following block within the configuration block:

<system.net>
<defaultProxy useDefaultCredentials="true"/>
</system.net>

Go to https://github.com/ezraundag/crmsvcutilextensions for the full codebase.



Comments are closed.