CrmSvcUtil Attribute Constant Generator Extension

A recent question on the CRM MSDN forum made me realise that as far as I can see, no-one has yet created a CrmSvcUtil extension to generate attribute constants. So I did.

Download the CrmSvcUtil extension at codeplex: CrmSvcUtil Generate Attribute Constants.

To use the extension:

  1. Copy GenerateAttributeConstants.dll into the directory containing CrmSvcUtil.exe
  2. Run CrmSvcUtil with the following parameters:
CrmSvcUtil.exe ^
/codecustomization:"GenerateAttributeConstants.CodeCustomizationService, GenerateAttributeConstants" ^
/codewriterfilter:"GenerateAttributeConstants.FilteringService, GenerateAttributeConstants" ^
/url:http://servername/orgname/XRMServices/2011/Organization.svc ^
/out:Attributes.cs

The baseTypes parameter is optional and can be used to override the base types for entity classes which is needed if you have previously generated the wrapper classes with an extension (eg. Microsoft.Xrm.Client.CodeGeneration.CodeCustomization).

To use the generated attribute constants:

Contact contact = new Contact();
contact.Attributes[Contact.firstnameAttribute] = "Michael";
contact.Attributes[Contact.lastnameAttribute] = "Palmer";
Advertisements

17 thoughts on “CrmSvcUtil Attribute Constant Generator Extension

  1. Pingback: CrmSvcUtil Option Set Enum Generator Extension | xrMPalmer

  2. It seems the attribute property is not marked as static once the class ıs generated.So I cannot access it as you have mentıon ın your post.
    Thanks

  3. Hi Mıke
    Sorry for the confusıon, seems I need to go back n refresh my programmıng conepts 🙂
    I was assumıng we cant access constant ıf ıts not marked as statıc.

    Thanks for respondıng.

    Cheers
    PB

    • Hey, well early bound entities generate classes with strongly typed attribute properties. This CrmSvcUtil extension generates constants for all the attributes on an entity so that they can be used when working with dynamic entities or any other time you need to reference entity attribute names (query expression column lists, etc).

      It avoids code like (notice the typo in the field name that would not cause errors until run-time)

      Entity account = new Entity(Account.EntityLogicalName);
      account["new_atribute"] = "Mike";
      

      in favour of code like

      Entity account = new Entity(Account.EntityLogicalName);
      account[Account.new_attributeAttribute] = "Mike";
      

      which comes with all the benefits of constants and compilation.

  4. Pingback: CrmSvcUtil Entity Generator Extension | xrMPalmer

  5. Pingback: CrmSvcUtil Extensions for CRM 2015 released | xrMPalmer

  6. Pingback: CrmSvcUtil Extensions for CRM 2016 released | xrMPalmer

  7. How generate code for attributes PickList?
    I have this attribute: rsg_tipodemediador, type Picklist.

    MetaData Document (XrmToolbox Plugin) generates:

    rsg_tipodemediador rsg_TipodeMediador Tipo de Mediador Picklist
    rsg_tipodemediadorname rsg_tipodemediadorName N/A Virtual N/A

    Your -great- generator extensión only generates:
    public const string rsg_tipodemediadorAttribute = “rsg_tipodemediador”;

    Not generates

    public const string rsg_tipodemediadornameAttribute = “rsg_tipodemediadorname”;

    Any suggestions?

    • Hi,

      To generate option set enumerations, you’ll need to use the CrmSvcUtil Option Set Enum Generator Extension.

      The rsg_tipodemediadorname property is a not an actual field – it is the calculated name property of the rsg_tipodemediador field. The CrmSvcUtil Attribute Constant Generator Extension does not generate constants for the calculated properties. If you have retrieved the rsg_tipodemediador field, you should be able to get to the name property of the field like so (in C#):

      ((OptionSetValue)entity[entityClass.rsg_tipodemediadorAttribute]).Name

      You’ll obviously have to do some null checking to avoid the expression above throwing a “Key Not Found” error if the field is null.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s