This is a second part of my last blog step-by-step-plugin-tutorial Developer’s Toolkit Part 1. In the last blog, we created a plugin using late binding. In this blog we use early binding (strongly typed classes) to create the same plugin. This is another advantage of using the Developer’s Toolkit. We can generate the strongly typed classes by click of the mouse. We don’t have to use CrmSvcUtil Command-Line Utility and copy the generated file to our plugin Project. I am going to use the same solution I have used for my last blog.
Please read the last blog before starting this one. Here are the steps.
  1. Open the CRM Explorer. Right Click on the Entities node and select “Generate  Wrapper”. It will generate the strongly type classes and add it to the Plugins project. The name of the generated file will be Entities.cs.c1 c3
  2. Now expand the Entities node. Right Click on Account Entity and select “Create Plug-in”. It will display a following dialog. This time we are creating a plugin on update event. Enter the values as shown the screen shot. You can change the class name if you like and press OK. It will create a new file “PostAccountUpdate.cs” in the plugins project.c4
  3. Open the “PostAccountUpdate.cs” file. Add the reference to Entites.cs file to use the strongly typed classes.
  4. Copy the ExecutePostAccountCreate() function code from PostAccountCreate.cs created in last blog and paste it in ExecutePostAccountUpdate() function of “PostAccountUpdate.cs”.
  5. Replace the following lines in copied code of ExecutePostAccountUpdate() function
    //create a task
    Entity task = new Entity("task");
    task["subject"] = "Account number is missing";
    task["regardingobjectid"] = new EntityReference("account",entity.Id);

    //adding attribute using the add function
    // task["description"] = "Account number is missng for the following account. Please enter the account number";
    task.Attributes.Add("description", "Account number is missng for the following account. Please enter the account number");

    // Create the task in Microsoft Dynamics CRM.
    service.Create(task);
    with
    //create a task
    Task task = new Task();
    task.Subject = "Account number is missing";
    task.RegardingObjectId = new EntityReference("account", new Guid(context.OutputParameters["id"].ToString()));
    task.Description = "Account number is missng for the following account. Please enter the account number";

    // Create the task in Microsoft Dynamics CRM.
    service.Create(task);

    We replaced the Entity object with Task object and we used task.Description in place of task["description"]. Early binding (strongly typed classes provides us all the crm entities objects, their properties and methods.
  6. Now right click on CRM Package project and select deploy. It will register the plugin assembly as well as step for the plugin. Click on CRM Explorer to check the deployed plugin as shown in the following screen shot. There are two plugins registered, one for post update event that we just created and one for post create event we created in last blog. Update an account to test the plugin.       c6

Advantages for using early binding

Here is the link to explain the differences between early binding and late binding. I am listing few of advantages using early binding.
  1. The biggest advantage of using early binding is that we have access to all the CRM entities(account, contact, opportunity, task etc) their attributes and their relationships.In late binding we use the Entity (dynamic entity) to work with all the CRM entities.
  2. Access to visual studio intellisense while writing code.In the following screen shot, we create an instance of Task entity and when we type Task, it will display all the attributes and methods belong to task entity.c5
  3. There are less chances of misspelling the attributes names. Early bound references are checked at compile time. for e.g if we spell the attribute name wrong, you won’t able to compile the code. In late binding, it will hard to find out if we misspell the attribute name as references will be checked at run time.