Getting Attribute Values in Plugin Execution Best Practice: Use Pre/Post Images

Yesterday I just discovered from a discussion of a question, what I’ve known in a couple of years is not the best practice. The question was about how to get the entity attribute values from the record where the plugin was fired. Based on my previous knowledge, it should part of the plugin execution context. Yes, it is true for some operations such as Create and Update (based on the fields that updated). However, my next approach: if I could not find it in the plugin context: get it through retrieve request, is completely wrong. It adds server overhead when I add another request just to get the entity attribute details.

After reading this MSDN article: “Understanding the data context passed to a plug-in“, there is a line under Pre/Post Entity Image section that mentioned: “Registering for pre or post images to access entity attribute values results in improved plug-in performance as compared to obtaining entity attributes in plug-in code through RetrieveRequest or RetrieveMultipleRequest requests.

I’ve used the pre and post image to tackle some situational requirements, but now I know that what I’ve done was not right. It is probably an obvious or a well-known knowledge for most of the developers, but in case there are others that still using bad pattern as I do (I learned it from others’ sample as well), I hope this could change the development pattern and deliver the most efficient way in plugin development.

Advertisements

3 thoughts on “Getting Attribute Values in Plugin Execution Best Practice: Use Pre/Post Images

  1. Good point. But the pre and post image only contains snapshot data, sometimes it might be necessary to retrieve the entity, as not all attributes come through. This may not be such a big issue for post-image, but definitely one to watch out for in pre-image.

  2. I am a newbee and wondering when exactly to use pre or post-entity images. I have learned that for create operations I must use preImages and for delete operations postImages to use, what are the reasons for these? Didn’t find any convincing answers yet.

Leave a Reply

Please log in using one of these methods to post your comment:

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