Generate Early Bound Classes for selected entities using CrmSvcUtil.exe in MS CRM v9.x

What is Issue?

There is CrmSvcUtil.exe to generate early bound entity classes. But this utility generates the classes for all the entities in the organization. But these entities are not required all the time. So how to generate classes for selected entities.

The Reason-

The Microsoft provided the CrmSvcUtil.exe utility in Microsoft.crmsdk.coretools which is used to generate early bound classes for entities, but there is no option to select particular entity or entities.

Solution-

CrmSvcUtil.exe is available in Microsoft.crmsdk.coretools package and you can download this package from nuget.

The simple solution is provided by Erik Pool for Ms CRM 2011 and this will work D365 CRM v9.x as well. Just follow the below –

Create a new C# class library project in Visual Studio called SvcUtilFilter.

Add the below assemblies to your class Library-

svcUtil1

Important reference is – CrmSvcUtil.exe
– This exe has interface ICodeWriterFilterService which needs to be implemented.
– Microsoft.Xrm.Sdk is mandatory
– Microsoft.Xrm.Tooling.Connector is important in case of v9.x.
– If you want to connect to your organization by interactive UI add
Microsoft.Xrm.Tooling.Ui.Styles and Microsoft.Xrm.Tooling.CrmConnectControl.

Add the following class to the project:

using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Microsoft.Crm.Services.Utility;
using Microsoft.Xrm.Sdk.Metadata;

namespace SvcUtilFilter
{
/// CodeWriterFilter for CrmSvcUtil that reads list of entities from an xml file to
/// determine whether or not the entity class should be generated.
public class CodeWriterFilter : ICodeWriterFilterService
{
//list of entity names to generate classes for.
private HashSet _validEntities = new HashSet();

//reference to the default service.
private ICodeWriterFilterService _defaultService = null;

/// constructor
public CodeWriterFilter(ICodeWriterFilterService defaultService)
{
this._defaultService = defaultService;
LoadFilterData();
}

/// loads the entity filter data from the filter.xml file
private void LoadFilterData()
{
XElement xml = XElement.Load("filter.xml");
XElement entitiesElement = xml.Element("entities");
foreach (XElement entityElement in entitiesElement.Elements("entity"))
{
_validEntities.Add(entityElement.Value.ToLowerInvariant());
}
}

/// /Use filter entity list to determine if the entity class should be generated.
public bool GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
{
return (_validEntities.Contains(entityMetadata.LogicalName.ToLowerInvariant()));
}

//All other methods just use default implementation:

public bool GenerateAttribute(AttributeMetadata attributeMetadata, IServiceProvider services)
{
return _defaultService.GenerateAttribute(attributeMetadata, services);
}

public bool GenerateOption(OptionMetadata optionMetadata, IServiceProvider services)
{
return _defaultService.GenerateOption(optionMetadata, services);
}

public bool GenerateOptionSet(OptionSetMetadataBase optionSetMetadata, IServiceProvider services)
{
return _defaultService.GenerateOptionSet(optionSetMetadata, services);
}

public bool GenerateRelationship(RelationshipMetadataBase relationshipMetadata, EntityMetadata otherEntityMetadata, IServiceProvider services)
{
return _defaultService.GenerateRelationship(relationshipMetadata, otherEntityMetadata, services);
}

public bool GenerateServiceContext(IServiceProvider services)
{
return _defaultService.GenerateServiceContext(services);
}
}
}

Build the project.

Next step to create XML which specifies the entities names for which you want to have early bound class.

Create filter.xml file with below structure-

<filter>

<entities>

<entity>account</entity>

<entity>contact</entity>

</entities>

</filter>

To add entity same way in this XML to create early bound class for it.

Add this filter.xml in same build folder debug/bin i.e. where the CrmSvcUtil.exe and all the other references located.

Now your build folder should be look like this-

svcUtil3

Your tool is ready now.

How to run this?
Open Command prompt. Change directory to the bin folder and run below command-

crmsvcutil.exe /url:http://your_org_url/XrmServices/2011/Organization.svc /out:output.cs /namespace: /il /codewriterfilter:SvcUtilFilter.CodeWriterFilter,SvcUtilFilter

(For more information about parameters execute command : crmsvcutil.exe ? )

This will open the UI to connect Organization-

svcUtil2

Connect to your Organization and then wait till your output.cs is ready.

You will be able to find output.cs file in same bin folder once command executed successfully. This file will be having early bound entity classes for your selected entities.

Hope this will help…

Enjoy Ms CRM!!!

Follow on Facebook- FB: MSCRM16Tech


					

5 thoughts on “Generate Early Bound Classes for selected entities using CrmSvcUtil.exe in MS CRM v9.x

Leave a comment