Monday, January 16, 2012

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

I was looking at developer’s toolkit shipped with CRM2011 SDK. There are few blogs out there on how to create plugins using developer's tool kit. Most of the blogs (more or less) are the copy of  “developer toolkit user's guide” that comes with the toolkit.

Here is link to Sam’s blog that explains each project in developer’s toolkit. MSDN blog also have similar contents. In this blog I am going to create a sample plugin using developer’s toolkit. It is a copy of one of my earlier blog(Step by step plugin tutorial for CRM 2011) .

Install the developer’s toolkit.The Developer toolkit for Microsoft Dynamics CRM 2011 was released as part of UR5 SDK release and is available for download here.


  1. Create a new solution in CRM2011. I named my solution “CRM Plugin Solution”. This is optional but I would recommend you do that.
  2. Open Visual Studio 2010. Select File—New –Project. It will display new project templates dialog as shown in the screen sheet belowp1
  3. Select “Dynamics CRM 2011 Package” project. This is also optional. You can go ahead and select “Dynamics CRM 2011 Plugin Library”, But then you cannot deploy the plugin straight from the Visual Studio. You have to use Plugin Registration tool to register the plugin. Enter the name of project/solution.
  4. VS studio will display following dialog.Enter you CRM 2011 server details.Select the solution name we created in step 1 and click okp2
  5. Now right click on the solution and add “Dynamics CRM 2011 Plugin Library” project to the solution. The plugin project will already have a plugin.cs file.
  6. Now sign the plugin assembly. Right click on Plugin Project and select properties. Select Signing tab from left navigation of project property page. On the Signing tab, select the Sign the assembly check box and set the strong name key file of your choice.At a minimum, you must specify a new key file name. Do not protect your key file by using a password.
  7. If You cannot see the “CRM Explorer” window on the upper left side of the VS studio, click on View menu and select “CRM Explorer”. p3
  8. Now expand “Entities” Node. Right Click the entity on want to create the plugin for and select “Create Plugin”.p4
  9. It will display a following screen.It is equivalent to “Create Step” screen in plugin registration tool. The dialog will pick up the name of the entity and other information. Choose the message and the pipeline stage. You can also change of the Class attribute. Press Ok.pn5
  10. It will create a .cs file with name mentioned in “Class” attribute in screen shot above. Double click on the class file(PostAccountCreate) and scroll down to following lines of code. You write your business logic here.
    protected void ExecutePostAccountCreate(LocalPluginContext localContext)
    {
       if (localContext == null)
       {
           throw new ArgumentNullException("localContext");
       }
       // TODO: Implement your custom Plug-in business logic.
    }
  11. The first thing you will do is to get the plugin context, CRMService instance and TracingService instance using localContext passed to the function. All these objects are defined in the built in plugin.cs class.
    IPluginExecutionContext context = localContext.PluginExecutionContext;
    IOrganizationService service = localContext.OrganizationService;
    //ITracingService tracingService = localContext.TracingService;
  12. Here is code. It will check if the “account number” is null or empty and  create a task for a user to enter the account number.
    protected void ExecutePostAccountCreate(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }
    
        // TODO: Implement your custom Plug-in business logic.
        // Obtain the execution context from the service provider.
        IPluginExecutionContext context = localContext.PluginExecutionContext;
        IOrganizationService service = localContext.OrganizationService;
        //ITracingService tracingService = localContext.TracingService;
    
    
        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parmameters.
            Entity entity = (Entity)context.InputParameters["Target"];
                    
            //EntityReference pp = entity.GetAttributeValue("primarycontactid");
            //tracingService.Trace(pp.LogicalName);
                   
                    
            try
            {
                //check if the account number exist
    
                if (entity.Attributes.Contains("accountnumber") == false)
                {
    
                    //create a task
                    Entity task = new Entity("task");
                    task["subject"] = "Account number is missing";
                    task["regardingobjectid"] = new EntityReference("account", new Guid(context.OutputParameters["id"].ToString()));
    
                    //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);
    
    
                }
            }
    
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            }
    
        }
    
    }
    I also left few commented lines in the code to show “How to use tracingservice to write in trace log”. You will able to see the trace only if there is an error in the plugin.
  13. Now right click on CRM Package project we created in step 2 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.pn1
  14. Create a new account and test the plugin.

55 comments:

  1. Nice tutorial on using the Developer's Toolkit Singh! I was just starting to look at the SDK when I ran across your post. It saved me quite a bit of time and simplified both the creation and deployment of several plugins!

    ReplyDelete
    Replies
    1. Thanks for the feedback. I m glad it helped.

      Delete
    2. Singh is king .. Great stuff Amreek

      Delete
  2. Thanks for the tutorial!
    When I attempt to create the plug-in by hitting the OK button from the "create plug-in" window in step 9, I get the error: "Length cannot be less than zero. Parameter name: length"
    Any help is appreciated.

    ReplyDelete
    Replies
    1. ...i needed to save the solution first. Sorry to bother! Great Tutorial!! thanks again.

      Delete
  3. Hi, I tried to use the code above with difference being in updating a record instead of creating one. However, when I tried to update, an error regarding infinite loop settings comes up. I did a bit of readup on this and have increased the setting to 100 (8 being default I read). However, the update just times out with an SQL error. Could you please point me in the right direction? Thanks heaps.

    ReplyDelete
    Replies
    1. If you are registering a plugin on post update event and updating the same entity. It is going to get stuck in the loop. I would suggest, if you are updating entity, which is triggering the plugin then register it on pre update.
      Other options are use Depth attribute of the context
      or
      use filtering as mentioned in my my blog http://mscrmshop.blogspot.com.au/2012/02/use-of-filtering-attributes-during.html
      I hope this helps.

      Delete
  4. Thanks for the good tutorial! I am unable to debug the plug-in exist in the solution. how I can debug the solution.

    ReplyDelete
  5. try these steps

    http://msdn.microsoft.com/en-us/library/gg328574.aspx

    I hope this helps.

    ReplyDelete
  6. Hi,
    how to connect the Online MS CRM 2011?, can u post an example screen shot for online MS crm 2011 connection details.

    thanks
    suray kathir

    ReplyDelete
  7. Put your URL like inv.crm5.dynamics.com,
    remove 80 from the port text box.
    choose https and press connect

    ReplyDelete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. Hi Singh
    Very nice tutorial, thanks for sharing .
    I have a question , can you please tell me where the plugin assembly will be register? will it be on the crm server or the database or somewhere else?
    I am asking because although i have the admin role in my organisation we (the org)have an "on-Premise" Licence and actually we are not allow to register plugins on the CRM Server since there are also other companies present on the Server. Surprisingly for me i was able to follow all the steps of this tutorial and register the plugin. I can see the registered Plugins unther the setting options with a Deployement type of sandbox

    Thanks a lot for your help

    ReplyDelete
  10. Sandbox is a isolated environment. You generally run the plugins in a sandbox where you don't have permission to deploy the plugin on the server or database foe e.g. mscrmonline.
    Have a look at this link http://msdn.microsoft.com/en-us/library/gg334752.aspx. In short, your plugin is running on the machine that is running a Microsoft.Crm.Sandbox.HostService.exe process.

    ReplyDelete
  11. Microsoft Dynamics CRM training will help you manage and prioritize your business goals, customize.we teach MS Dynamics CRM training and class available at Hyderabad.

    ReplyDelete
  12. Hi when i try to deploy i get

    Error 1 Error connecting to CRM Server. The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs. D:\Program Files (x86)\MSBuild\Microsoft\CRM\Microsoft.CrmDeveloperTools.CrmClient.targets

    ReplyDelete
    Replies
    1. Did you ever find a resolution to this, I am now getting the same error?

      Delete
    2. This comment has been removed by the author.

      Delete
  13. After building the app, when tries to deploy, getting following error.

    Error registering plugins and/or workflows. Plug-in assembly does not contain the required types or assembly content cannot be updated.

    ReplyDelete
    Replies
    1. had u get the reason ...i am getting same error

      Delete
    2. Try this
      http://gonzaloruizcrm.blogspot.com.au/2012/08/crm-2011-plug-in-assembly-does-not.html

      Thanks

      Delete
  14. How to install Developer Toolkit for VS 2012 RC? Actually, I've installed it successfully, but there is no "Dynamics CRM" node in my project templates dilog box.

    ReplyDelete
  15. When I try to deploy , it gives me following error:

    "Error registering plugins and/or workflows. An unexpected error occurred. ...\Microsoft.CrmDeveloperTools.CrmClient.targets"

    Please help me out.

    ReplyDelete
  16. thanks a lot for this tutorial. I am beginners to words ms crm. This tutorial give a good idea on plugin with in few time....


    thanks again :)

    ReplyDelete
  17. Hi,
    I want to retireve the "names" of existing account's from online crm. How do i do this?? Please help me.....

    ReplyDelete
  18. Truly remarkable labor with the blog. I do like your inflexible service and will wait for more post from you as post gave me gratification and gives some helps to do same work right here. Thanks a lot.Private equity Placement

    ReplyDelete
  19. Hi,
    Is there any way to change the deployment target of the existing package project? For e.g., currently I have pointed it to my development server and now I want to deploy it on my QA server....how to change the deployment target?

    Thanks!

    ReplyDelete
  20. 5. Now right click on the solution and add “Dynamics CRM 2011 Plugin Library” project to the solution. The plugin project will already have a plugin.cs file.
    6. Now sign the plugin assembly. Right click on Plugin Project and select properties. Select Signing tab from left navigation of project property page. On the Signing tab, select the Sign the assembly check box and set the strong name key file of your choice.

    Can you explain these steps? I can not find this. My degree of knowledge in developing plugins, this is my first one.

    ReplyDelete
    Replies
    1. I have the same question,

      Delete
  21. I have an issue with CRM Explorer, I am giving server name , username,password,domain name and selecting organization...but the solution drop-down is disabled and as soon as I select organization the dialogue box is getting disappeared. Any help to resolve this please...

    ReplyDelete
  22. Hi, thanks for this great stuff, excellent post regarding to microsoft dynamics crm tutorial.

    ReplyDelete
  23.  Hey thanks for informing me about the basic steps in a developer toolkit.........This was really helpful for me. 
    CRM Development

    ReplyDelete
  24. Hi Im new to CRM and have been tryng to adapt your brilliant tutorial so that when a record is created in a contatc entity, a csv file is generated. gan you give me any pointers on this

    ReplyDelete
  25. Thank you sharing this information thank you.Come back again for more interesting stuffs like one you got earlier! Bulk SMS in India

    ReplyDelete
  26. Hi, I'm using VS2012 and when deploying my plugin from VS, none of my steps are been registered and if I right-click on my now deployed plugin in the CRM Explorer, the "add step" functionality doesn't exist. Any thoughts? Thanks.

    ReplyDelete
    Replies
    1. VS2012 and the DevKit didnt register my plugins properly, had to use the Plugin Reg Tool.

      Very strange.

      Have any thoughts as to why VS didn't register my plugins?

      Thanks

      Delete
  27. Thanks so much for this helpful information come back again for more interesting information…Keep it up
    Builders in Agra

    ReplyDelete
  28. Nice blog! Very interesting and informative post….Thank's for sharing such a nice information….Keep it up!!! Website Designing in Agra

    ReplyDelete
  29. Hi, I was following the steps for Plugin Development in CRM in your post and it's very easy to follow.

    Thanks a lot

    ReplyDelete
  30. Nice tutorial, but no idea how to perform step 5 :
    "Now right click on the solution and add “Dynamics CRM 2011 Plugin Library” project to the solution. The plugin project will already have a plugin.cs file."
    Do you mean, add New Item, Add existing item or Add reference ?

    ReplyDelete
    Replies
    1. Solved it and works like a charm !!
      Before perfoming step 5
      - make sure you've also created an empty plugin-project
      - in VS2012 be sure to check : options -> Projects and solutions -> General -> Always show solution !!
      - now you can perform step 5 right-click on solution -> add.. -> existing project -> pick the empty plugin project you've created for this purpose

      Delete
  31. Hey nice blog,Thank's for this helpful information come back again for more interesting information…Keep it up!
    Taxi in Agra

    ReplyDelete
  32. Hi Amreek nice article, Thank's for this helpful information

    ReplyDelete
  33. very nice blog !
    certainly nice place to learn how to create a first Plugin.

    ReplyDelete
  34. Hey nice blog,Thanks a lot for this useful and helpful information.Thanks a lot for this tutorial. I found some good information on https://www.evello.com.au/.

    ReplyDelete