Extending CrmSvcUtil.exe

Ezra Undag
2 min readApr 24, 2019

--

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. I added the class EntityFilter to Sebastian Holager’s CRMSvcUtilExtensions solution on Github. EntityFilter 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.

References

CRMSvcUtilExtensions

Basics Guide for Dynamics 365 for Customer Engagement apps

Create early bound entity classes with the code generation tool (CrmSvcUtil.exe)

This article is reposted in http://www.ezraundag.com/technology/extending-crmsvcutil-exe/.

--

--