Example T4 template (C#)

<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Windows.Forms.dll" #>
<#@ Assembly Name="CrmCodeGenerator.Core.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="CrmCodeGenerator.Core" #>
<#@ import namespace="CrmCodeGenerator.Core.Model" #>
<#@ import namespace="CrmCodeGenerator.Core.Naming" #>
<#@ import namespace="CrmCodeGenerator.Core.T4" #>
<#@ property processor="PropertyProcessor" type="CrmCodeGenerator.Core.Model.Context" name="Context" #>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
 
namespace <#= Context.Namespace #>
{
<# foreach (var entity in Context.Entities)
{
#>
    public partial class <#=entity.DisplayName#>
    {
        public class Enums
        {
<#foreach(var enm in entity.Enums){#>
            public enum <#=enm.DisplayName#>
            {
<#foreach(var item in enm.Items){#>
                <#= item.Name #> = <#= item.Value #>,
<#}#>
            }       
<#}#>
        }
 
        public class Fields
        {
            public const string SchemaName = "<#=entity.Attribute.LogicalName#>";
 
<#foreach(var f in entity.Fields){#>
            public const string <#=f.DisplayName#> = "<#=f.Attribute.LogicalName#>";
<#}#>
        }
 
        public override Guid PrimaryKey
        {
            get { return <#=entity.PrimaryKeyProperty#>; }
        }
 
<#
    foreach(var attribute in entity.Fields)    {#>
        public virtual <#=attribute.TargetType#> <#=attribute.DisplayName#> { get; set; }
<#}#>
    }
<#}#>
}

 

Dynamics CRM 2011 T4 Code Generator

The generator comes with two T4 templates. One for C# and another for Javascript. These two templates can be used as a base for your custom templates. We are still in the process of creating a template that matches crmsvcutil.exe.

To get started, double click on CrmCodeGenerator.UI.exe.Dynamics CRM 2011 T4 Code Generator

Discovery url can be found by going into the Customizations area > Developer Resources. If you're connecting to CRM online please leave the domain name blank.

Once you click next you'll need to specify the T4 template path, output path, entities to include/exclude.Dynamics CRM 2011 T4 Code Generator - Step 2

To control which entities are included in the code generation use a combination of Entities to include and exclude. For example:

To generate code for ALL entities set the Entities to Include to * and leave Entities to Exclude blank.
To generate code for account and contact set the Entities to include to "account,contact" and set Entities to Exclude to *

Click Next to begin the code generation. Retrieving metadata from CRM 2011 will take a few minutes.Dynamics CRM 2011 T4 Code Generator - Retrieve Metadata

Last edited Aug 9, 2012 at 1:53 AM by gperera, version 3

Comments

gperera Mar 13, 2013 at 8:39 AM 
Hi crmcodegen, the main difference is with the T4 you get control over what gets generated very easily without having to extend crmsvcutil using .net code.

crmcodegen Mar 7, 2013 at 1:27 PM 
please provide the info on crmsvcutil vs Dynamics T4 code generator

AspireAlex Aug 23, 2012 at 2:22 PM 
Looking good! I look forward to the release in September :)