Wednesday, January 18, 2012

Step by step plugin tutorial using Developer's Toolkit Part 2

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.

9 comments:

  1. Hi Amreek,

    I have to access Nav web service in crm to pass data from crm 2011 to nav wiht crm values like name ,city ,email etc.
    Please help for that.

    ReplyDelete
  2. Hi Singh,

    please can you tell me how can i add the Entities reference in my code?

    Thanks

    ReplyDelete
  3. Hi,
    i have a an exception by trying this code saying the givin key"id" was not present in the dictionnary, the create post event plugin is working fine
    Please what is causing this error? why is the plugin no longer able to retrieve the id?

    Thanks for your reply

    ReplyDelete
    Replies
    1. the problem is this line
      task.RegardingObjectId = new EntityReference("account", new Guid(context.OutputParameters["id"].ToString()));
      this will work for post create but not for post update. change that line with
      task.RegardingObjectId =new EntityReference("account",entity.Id);

      I hope this helps.

      Delete
  4. Hey Singh,

    Please can you tell me how can i add the Entities reference in my code?

    Does my plugin class has to inherit the Entities class?

    ReplyDelete
  5. just add using Entities; at the top of your plugin file

    ReplyDelete
  6. Hi,

    Can you please help with my requirement.

    Requirement: in account entity when an oppurtunity status is open.user shouldnt deactivate the account by using plugin.(please share the code)

    please help with this.

    ReplyDelete