How to automatically generate early binding properties for an Entity-specific (i.e.Local) option. Set text values?

After spending a year working with the Microsoft.Xrm.Sdk namespace, I just discovered that the Entity.FormattedValues property contains a text value for Entity special objects (i.e. Local) yesterday.

The reason I didn't find it before is because the early binding method doesn't matter. that is, entity.new_myOptionSet is of type OptionSetValue , which contains only an int value. You must call entity.FormattedValues ​​["new_myoptionset"] to get the string text value for OptionSetValue .

Therefore, I would like to get crmsrvcutil to automatically create a text property for local parameter sets. those. along with entity.new_myOptionSet generated the way it currently does, Entity.new_myOptionSetText will also be generated.

I looked at Microsoft.Crm.Services.Utility.ICodeGenerationService , but it looks like it is mainly to indicate that CodeGenerationType should be something ...

Is there a way that supports the use of CrmServiceUtil to add these properties, or is it better for me to write a custom application that I can run that can generate these properties as a partial class for automatically generated ones?

Edit - sample code that I would like to generate

Currently, when I need to access the text value of the OptionSetValue parameter, I use this code:

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

The options installation cache will use entity.LogicalName , and the property expression will determine the name of the parameter set I'm asking for. He will then query the SDK using RetrieveAttriubteRequest to get a list of set int options and text values, which he then caches, so he doesn't need to hit CRM again. He then looks at the int value of the New_MyOptionSet object and cross-references it with a cached list to get the text value of the OptionSet.

Instead of doing all this, I can just do it (assuming the object was received from the server, and not just from the filled client side):

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

but "new_myoptionset" is no longer early. I would like the classes of the early linked entity that are generated to also generate an additional β€œText” property for the OptionSetValue properties that calls the above line, so my object would add the following to it:

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

The latest version of CRM Early Bound Generator has a Fields structure that contains field names. This allows you to access FormattedValues ​​as easy as this:

 var textValue = entity.FormattedValues[MyEntity.Fields.new_MyOptionSet]; 

You can create a new property through the interface for CrmSvcUtil, but a simple call is enough for work, and I do not think that it justifies the creation of additional properties.

0
source

Could you use the CrmServiceUtil extension, which will generate enums for your OptionSets, and then add the new_myOptionSetText property to a partial class that compares the int value with the enums and returns an enumeration string

0
source

Again, I think, specifically for this case, getting CrmSvcUtil.exe to generate the code you want is a great idea, but more generally you can access the property name through reflection using an approach similar to the accepted answer @ workarounds for the nameof () operator in C #: databinding data types .

 var textValue = entity.FormattedValues["new_myoptionset"]; // becomes var textValue = entity.FormattedValues [ // renamed the class from Nameof to NameOf NameOf(Xrm.MyEntity).Property(x => x.new_MyOptionSet).ToLower() ]; 
0
source

Source: https://habr.com/ru/post/1443444/


All Articles