CRM Crate

Understanding plugin images in Microsoft Dynamics CRM

We will learn what is an image in Microsoft Dynamics CRM and what is its usage within the plugin. Before we start, make sure you have basic knowledge on plugins by going through the below link.

images in Microsoft Dynamics CRM

What is an Image?

In Microsoft Dynamics CRM, plugin images are snapshots of an entity object of entity attribute data, before and after of system operation execution. In other words, the images are same and the target entity object which we fetch during the plugin execution, the only difference here is the images can contain entity attribute information before the record is been saved and attribute information after saving the data.

Different types of Images

Two types of images are available which are “Pre-Image” and “Post-Image”. As the name describes, the Pre-Image is used to store the attribute values which were present before saving the record (SQL Database Transaction) while the Post-Image is used to store the attribute values which where are present after saving the record (SQL Database Transaction).

Event pipeline using different Images

Message Name Stage Name Pre-Image Post-Image
Create PRE-OPERATION No No
Create POST-OPERATION No Yes
Update PRE-OPERATION Yes No
Update POST-OPERATION Yes Yes
Delete PRE-OPERATION Yes No
Delete POST-OPERATION Yes No

Registering Images in plugin

In order to implement the images in your plugin code, first you need to register the plugin within for your plugin step by using Plugin Registration Tool.

Open the Plugin Registration Tool >> Select the plugin step in which you want to add the images >> Right click on the step and click on “Register New Image”.

Once the image window is populated, you can now select which type of image you want to get associated with you plugin. Here, we have to give a unique name to our image so that our plugin code can identify it. In our scenario, we have added both Pre-Image and Post-Image by given them a unique name as “PreEntityImage” and “PostEntityImage”.

Later, we have an option to select the number of attributes which we want to store in our image. After configuring the image, click on Ok.

How to use an image in plugin code?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace CRM_Crate___My_Sample_Plugin
{
    public class Plugin : IPlugin
    {
        //Main Execute Method.
        public void Execute(IServiceProvider serviceProvider)
        {
            //Initializing Service Context.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            try
            {
                //Defining Entity Object.
                Entity eTarget = null;
                if (context.MessageName == "Create")
                {
                    //Retrieving Target Entity.
                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?
                    context.InputParameters["Target"] as Entity : null;
                    //Stage 1 - Entity Variable Creation.
                    Entity preTarget = new Entity();
                    Entity postTarget = new Entity();
                    Entity preImage = new Entity();
                    Entity postImage = new Entity();
                    //Stage 2 - Validating & Declaring Images.
                    if (context.PreEntityImages.Contains("PreEntityImage"))
                    {
                        preImage = context.PreEntityImages["PreEntityImage"];
                        preTarget.Attributes = preImage.Attributes;
                    }
                    if (context.PostEntityImages.Contains("PostEntityImage"))
                    {
                        postImage = context.PostEntityImages["PostEntityImage"];
                        postTarget.Attributes = postImage.Attributes;
                    }
                    //Stage 3 - Retrieving Data Stored In Pre-Image & Post Image.
                    string preAccountName = preTarget.GetAttributeValue("accountname");
                    string postAccountName = postTarget.GetAttributeValue("accountname");
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }
}

Explanation of the above code is given below.

  • Stage 1 : We have created entity objects for storing our images.
  • Stage 2 : Validating whether the registered step is having plugin with given unique name. If the plugin step contains image with the particular name then store the image attributes in above created entity objects.
  • Stage 3 : Here, we have retrieved data or field value from the image attributes.

Thus, we have learnt what is an Image and how to use it inside a plugin.

0 0 votes
Article Rating
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Read, update, create and delete record using plugins - CRM Crate
1 year ago

[…] Understanding images in plugin […]

error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.

Practise EXams

Free!!

Sharpen your skills with our free practise exams.