Wednesday, March 20, 2013

Plugin Walkthrough using Visual studio 2012

Plugin Walkthrough using Visual Studio 2012.

 
Visual studio 2012 makes our life very easy after giving a Dynamics CRM toolkit that could be easily installed and could be used as mentioned here. After installing toolkit Visual studio gives us a new project type of Dynamics CRM. Here are the steps to create and deploy plugin for CRM 2011

Step 1:
Open visual studio click File-> New -> Project and From navigation menu select Dynamics CRM then select Dynamics CRM 2012 plug-in library and press OK.

Step 2:
Enter the credentials and press log on and if you successfully login you will see your organizations in drop down. Select your organization from dropdown and select solution from solution name dropdown box and press ok.
Step 3:
After pressing ok your project will created next step is to right click on your project and click on Properties as mentioned in above fig.
You will see a window with project properties and then click Signing and check Sign the assembly button enter your key file name and uncheck Protect my key file with a password check box.
Step 4:
Next step is to create Entities wrapper.
Right click on Entities in CRM Explorer bar and click on generate wrapper.
It will automatically generate a wrapper class for us we need to add the reference in our class only.
Open your Plugin.cs file and paste following code
Step 5:
 




using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using PluginWalkthrough.Entities;
 
public class Plugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)
        serviceProvider.GetService(typeof(IPluginExecutionContext));
 
        Entity entity;
 
        // Check if the input parameters property bag contains a target
        // of the create operation and that target is of type Entity.
        if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
        {
            // Obtain the target business entity from the input parameters.
            entity = (Entity)context.InputParameters["Target"];
 
            // Verify that the entity represents a contact.
            if (entity.LogicalName != "contact") { return; } // Write your entity name in which you want to trigger your plugin
        }
        else
        {
            return;
        }
 
        try
        {
            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(
            typeof(IOrganizationServiceFactory));
            IOrganizationService service =
            serviceFactory.CreateOrganizationService(context.UserId);
 
            var id = (Guid)context.OutputParameters["id"];
           
            //Write your business logic here
        }
        catch (FaultException<OrganizationServiceFault> ex)
        {
            throw new InvalidPluginExecutionException(
            "An error occurred in the plug-in.", ex);
        }
    }
 
 
}
Here you go your plugin is ready to build and for deploy. You can add your business logic in the plugin.

For deploying a plugin please check following link.
http://msdn.microsoft.com/en-us/library/gg309580.aspx


I hope it helps some one :)
 
 
 
 

No comments:

Post a Comment