How to auto-generate early bound properties for Entity specific (ie Local) Option Set text values?

Posted by Daryl on Stack Overflow See other posts from Stack Overflow or by Daryl
Published on 2012-11-01T13:39:46Z Indexed on 2012/11/03 17:02 UTC
Read the original article Hit count: 168

After spending a year working with the Microsoft.Xrm.Sdk namespace, I just discovered yesterday the Entity.FormattedValues property contains the text value for Entity specific (ie Local) Option Set texts.

The reason I didn't discover it before, is there is no early bound method of getting the value. i.e. entity.new_myOptionSet is of type OptionSetValue which only contains the int value. You have to call entity.FormattedValues["new_myoptionset"] to get the string text value of the OptionSetValue.

Therefore, I'd like to get the crmsrvcutil to auto-generate a text property for local option sets. i.e. Along with Entity.new_myOptionSet being generated as it currently does, Entity.new_myOptionSetText would be generated as well.

I've looked into the Microsoft.Crm.Services.Utility.ICodeGenerationService, but that looks like it is mostly for specifying what CodeGenerationType something should be...

Is there a way supported way using CrmServiceUtil to add these properties, or am I better off writing a custom app that I can run that can generate these properties as a partial class to the auto-generated ones?

Edit - Example of the code that I would like to be generated

Currently, whenever I need to access the text value of a OptionSetValue, I use this code:

var textValue = OptionSetCache.GetText(service, entity, e => e.New_MyOptionSet);

The option set cache will use the entity.LogicalName, and the property expression to determine the name of the option set that I'm asking for. It will then query the SDK using the RetrieveAttriubteRequest, to get a list of the option set int and text values, which it then caches so it doesn't have to hit CRM again. It then looks up the int value of the New_MyOptionSet of the entity and cross references it with the cached list, to get the text value of the OptionSet.

Instead of doing all of that, I can just do this (assuming that the entity has been retrieved from the server, and not just populated client side):

var textValue = entity.FormattedValues["new_myoptionset"];

but the "new_myoptionset" is no longer early bound. I would like the early bound entity classes that gets generated to also generate an extra "Text" property for OptionSetValue properties that calls the above line, so my entity would have this added to it:

public string New_MyOptionSetText {
    return this.GetFormattedAttributeValue("new_myoptionset"); // this is a protected method on the Entity class itself...
}

© Stack Overflow or respective owner

Related posts about dynamics-crm-2011

Related posts about crmsvcutil