Wednesday, May 16, 2012

CRM 2011 Plug-in to create a record using Developer Toolkit.

The Developer Toolkit for Microsoft Dynamics CRM 2011 represents integration with Visual Studio and focused on accelerating the development. Developer Toolkit for Microsoft Dynamics CRM was released as part of latest SDK release and is available for download here.

Once you downloaded the latest SDK, go to sdk\tools\developertoolkit and install crmdevelopertools_installer.msi. Then, to start your development with Visual Studio 2011,
Open Visual Studio 2011 à File à New à Project.  You can see the window as in the below figure (a).

Select Dynamics CRM under Visual C# in Installed Templates à Select New Visual Studio Solution Template for Dynamics CRM 2011 à Give the Name say, 'RcordCreate'  à OK.


Visual Studio 2011 solution Creation window
(a). Solution Creation for Dynamics CRM 2011 in Developer Toolkit.


You will be asking to fill 'Connect to Dynamics CRM Server' window to connect to the Server as you seen in the below figure (b) contains,

CRM Discovery Server Name - Name of the Server you are working on.
Port Number - Port Number, if any.
Protocol - Select HTTP or HTTPS protocol as per your server URL
Click 'Connect'.

  • If the details you provided is valid, then 'Authentication Details' section will get enabled.
  • Give UserName, Password and Domain, then click 'Log On'.
  • If the Authentication Details are valid, then 'Organization' dropdown will get enabled with the list of organizations present in the given server. Select the organization you are working on.
  • All the solutions present in selected organization will be listed in 'Solution Name' dropdown. Select the solution name which you want to store the plug-in assembly and click OK.

CRM Server Connection windo
(b). Connect to Dynamics CRM Server Window

Click Cancel if will be asking for New Silverlight Application.

Now you can see Visual Studio 2011 solution with three projects called CrmPackage, Plugins and Workflow. As of now we are not working with Workflow, you can remove Workflow project from the solution.
Under Plugins project, you can see Plugin.cs file, which contains default operations to be performed in plugins like IServiceProvider, IOrganizationService, IPluginExecutionContext and ITracingService.

Solution Explorer for CRM 2011 solution
(c). Solution Explorer for Dynamics CRM 2011 solution

Now we need to sign this plugin assembly with a .snk file.
Right click on Plugins project à Properties à Select 'Signing' in left tab à Select 'Sign the Assembly' check box à Select <New...> in 'Choose a strong name key file' drop down list à Give Key file name in Create Strong Name Key window à Uncheck 'Protect my key file with a password' (if you don't want to protect it with password) à OK à Save.

Lets start plug-in development to create an Account record on create of Lead with the Rating field is Hot.
In Visual Studio 2011, go to View à CRM Explorer à Expand organization name à Expand Entities à Right click on Lead à Create Plug-in. 
Create Plug-in window will be opened as in below figure. And select the values as below.
Message Name - Create
Run in Context - Calling User
Pipeline Stage - Post-Operation
Click OK.


Create Plug-in
(d). Create Plug-in Window to register steps.

Open PostLeadCreate.cs file created under Plugins project.
Go inside ExecutePostLeadCreate() method and replace //TODO: Implement your custom Plug-in business logic. with the code below and save the solution.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
IPluginExecutionContext context = localContext.PluginExecutionContext;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) {
    Entity lead = (Entity)context.InputParameters["Target"];
    try {                    
        if ( lead.LogicalName == "lead" && lead.Attributes.Contains("leadqualitycode")) {
            if (((OptionSetValue)lead.Attributes["leadqualitycode"]).Value.ToString() == "1") {
                IOrganizationService service = localContext.OrganizationService;
                Entity account = new Entity("account");
                account["name"] = "Qualified Lead-" + lead.GetAttributeValue<string>("companyname");                            
                service.Create(account);
            }
        }
    }
    catch (Exception ex) {
        throw new Exception("Error in PostLeadCreate Plug-in:" + ex.Message);
    }
} 

To build a solution, right click on the CreateRecord solution à Build Solution
To deploy the assembly to CRM system, right click on the CreateRecord solution à Deploy Solution
Once the deployment done, you can see the assembly and SDK Message Processing Step inside the solution you given while connecting to server. Go to CRM system and create one Lead record with Rating field as 'Hot'. Then you can see the Account record created with Account Name as "Qualified Lead-<Company Name>".

I t s   D o n e . . . J

1 comment:

  1. Thank you for your valuable comment, and its my pleasure. Definitely you will get more posts soon..

    ReplyDelete