Early Binding of PreImage in Microsoft CRM 2011
-
26-10-2019 - |
سؤال
The Microsoft CRM advanced developer extensions have gotten me a little spoiled with their early binding for calls made to CRM's webservices.
I'm writing a plugin right now and I'd like to access attributes defined in the pre-image. All the examples cast the preimage as Microsoft.Xrm.Sdk.Entity which uses late binding to access it's attributes. I dislike hardcoding all those string's for attribute names into my plugin and would like to find a method that avoids it by using early binding.
Here is an example of a cast
var preMessageImage = (Microsoft.Xrm.Sdk.Entity)context.PreEntityImages["MyPreImage"];
But I have to use late binding to access the properties
var myProperty = preMessageImate.Properties["MyProperty"];
Is there any way to cast this preimage to an xrm object that has all the properties defined using early binding so I don't have to hardcode all the property names?
المحلول
You should first use the crmsvcutil tool in the SDK to generate "early-bound" Xrm entities and include that code file in your plugin codebase.
I suggest omitting the 'DataContextName' command-line arg so not context is generated.
For more information check here on MSDN: CrmSvcUtil on MSDN
Next, you should use the ToEntity<T>
method on the Entity class to get a strongly-typed specific entity. More info here: ToEntity on MSDN