How To Access Attributes Within Plug-In Code
When developing plug-ins, users can access fields on the form before and after the record has been updated, created, etc. Developers can do this by first registering the assembly, then a step (configuring whether it's a post/pre update/create/etc., then finally by registering a pre/post image. The image(s) is/are passed in with the execution context. When configuring the image, one can select attributes that get passed in.
When developing the actual plug-in, these attributes can be accessed in the following manner:
entityImageName = (DynamicEntity)context.PostEntityImage[<postEntityImageName>];
attributeName = (DataType)entityImageName[<attributeName>].Value;
For example, if the attribute is a lookup…
DynamicEntity entityImageName = (DynamicEntity)context.PostImageEntity["testImage"];
Lookup testLookup = (Lookup)entityImageName["testLookupAttributeName"].Value;
If you look at the above code, the context has access to the pre/post images based on whatever you configured using the plug-in registration tool. The name you configured is the "postEntityImageName," This is then all cast to a DynamicEntity. Then you can access the attributes, much like an array or collection, using the attribute name. However, since the ".Value" method returns an object, it must be cast to the appropriate data type. The data type can be found in the customization section on the CRM client, or when registering a plug-in image, the data types are found when you need to select/de-select attributes to pass in.
State Code Attribute
For most attributes, the data types are straight forward. The data type for the id of the record is a Key, for any referential or parental relationship attributes, the data type is a Lookup, and for the Status attribute, the data type is simply Status. However, the State Code can be tricky and after trial and error, I've found that when you need to access the State Code attribute, you must cast this as a String. The two possible values that is out-of-the-box is "Active," and "Inactive."