Hardcore CRM: Birth of a C# CRM code generator

Almost year ago I started this blog. Today I’m writing the 100th article on this blog. For me this is a moment worth celebrating.

Looking back at last year, I dug up some statistics on JourneyIntoCRM.com that I want to share with you:

  • 1 year of blogging
  • 1 Blogger
  • drinking at least 34 litres of coffee while blogging
  • spending over 400 hours on 100 blog articles
  • articles containing over 70.000 words with an average of 125 Kilobytes of data per page
  • serving over 125.000 visitors and more than 600.000 pages
  • resulting in more than 72 Gigabytes of data traffic
  • consuming 192 KWh of power

It was definitely worth it…

For me a celebration is no celebration without any gifts. Therefor inspired by my last article, I decided to create a small gift for you.

Over the last days I’ve been working on an  intelligent code generator that you can use to speed up your software development projects. And the great thing is that I offer it as a free to use download (no strings attached).

Why this generator?

As most of you know, the CRM SDK offers the CrmSvcUtil.exe. A command line application that generates a humongous C# file with over 200.000 lines of code (6.158 KB) containing strong typed classes for all entities present in the Dynamics CRM organization.

For me this is sheer overkill: over 200.000 lines of unused code for entities that I don’t use!

When developing software, I’m only interested in the entities that belong to my solution. Nothing more, nothing less!

To put things in perspective: I created a small solution containing four entities (Account, Appointment, Customer Address and a custom entity).
I ran the CRMCodeGen against this solution and it resulted in four small strong typed data classes with a total of 1.400 lines of code (90 KB).

The CRMCodeGen generator creates data classes for all entities within your CRM solution. It connects to both CRM on-line and CRM on-premise.

The data classes generated are strong typed classes that map to the physical CRM entities.

Great, how does this thing work?

The generator is operated via the command prompt and can be easily embedded in an automated build process.

Crmcodegen1

On the command prompt you have a number of parameters that you can enter.

Syntax:

CrmCodeGen -Path: <Path name> -Url: <Url CRM Organization>
-Solution: <Solution name> -NameSpace: <NameSpace>
-NewFile: <true/false> -UserName: <UserName>
-Password: <Password> -Prefix: <prefix>

  • -Path: <Path name>
    Output path to which the files will be written e.g.  c:\temp.
  • -Url: <Url CRM Organization>
    Url of the CRM organization, e.g. http://localhost/development.
  • -Solution: or -S: <Solution name>
    Unique name of the CRM solution, e.g. MySolution.
  • -NameSpace: or -NS: <Namespace>
    Namespace of generated classes.
  • -NewFile: or -New: <true/false>
    Optional: if set to false, all classes will be written to Entities.cs.
    Default value = true, resulting in one file per class.
  • -UserName: or -User: <UserName>
    Optional: name/email address of the CRM user.
    If omitted, the Windows user will be used to connect.
  • -Password: or -Pwd: <Password>  
    Optional: password of the CRM user.
    If omitted, the Windows user will be used to connect.
  • –PublisherPrefix: or -Prefix: <Prefix>
    Optional: the publisher prefix used in CRM, e.g. jcrm_  or jcrm.
    If specified, the prefix is stripped from the entities and attributes, resulting in cleaner code.

The results

The generator results in data classes that can be added to your Visual Studio  C# project. Once added the generated data classes can be used like in the example below:

Crmcodegen2

Picklist options are translated into Enumerations (this applies as well for the statuscode).

In order to initialize the class, the static method “ClassName.InitializeFromEntity(entity)” is called, resulting in a class instance.
When writing the class back to CRM, you have to call the “class instance.ToEntity()” method. This method returns a CRM Entity containing the modified properties.

I hope you will enjoy this tool. In case of suggestions, questions or problems don’t hesitate to contact me.

Download: JourneyIntoCRM.CrmCodeGen_1_0_0_0.zip (321 KB)

2 thoughts on “Hardcore CRM: Birth of a C# CRM code generator

  1. Daryl LaBar says:

    The early bound generator for the XrmToolBox already does this without having to use the weird static factory method or having to convert back to a CRM entity.

    • Hi Daryl,

      it is good to see there are other tools out there. In a next version I want to make the generator template driven. That way you can generate code the way you want it to (not just my way 😉 )

      Bas

Leave a Reply

Your email address will not be published. Required fields are marked *