Monday, March 5, 2012

Plug Ins in CRM in 4.0.


A Plug-Ins is a .NET assembly that can be used to intercept events generated from the CRM system to perform a variety of actions. An example of event interception is an entity that will be created, updated, or deleted; an action can be virtually anything. Some common Plug-In uses might include these:
Performing a complicated update routine on CRM entities and/or attributes when it might be impractical to use JavaScript or Workflow
Grabbing data from another system and updating CRM when an entity instance is created or updated
Updating another system programmatically from CRM (such as an accounting system)

-Using Plug-Ins, you can fire a custom action or event on an Account, for example, either before or after it has been created, updated, or deleted. Additionally, other events can be handled from a Plug-In, such as Assign, Merge, and Handle.

When Is It Recommended to Use a Plug-In?
Use a Plug-In when you need to integrate Microsoft Dynamics CRM with another legacy system or when you want to extend or customize the original functionality or behaviors of Microsoft Dynamics CRM.

Modes
You can set up Plug-Ins in synchronous or asynchronous mode.
Synchronous mode starts the execution of the Plug-In when the event is fired and blocks the CRM application process until the executed method finishes. This option is not recommended if you perform a process that might take a long time to execute.

Asynchronous mode can't be combined with the Pre stage.

Stages
Plug-Ins can be set up in the Pre or Post stages:

The Pre stage sends control to the Plug-In before the real event is executed in the core system. As an example, you might attach a Plug-In to the Create event of the Account entity; your code would execute before the Account is actually created in the CRM system. With this method, you could prevent the Account or the entity record from being created, if desired.

The Post stage is executed after the real event has executed in the core system. So following the example just described, your code would be executed only after the Account record is created.

Plug-In Development
-To develop a Plug-In, you must download the Microsoft Dynamics CRM 4.0 SDK from the Microsoft website. The SDK can be found at http://www.microsoft.com/downloads/details.aspx?familyid=82E632A7-FAF9-41E0-8EC1-A2662AAE9DFB&displaylang=en or by searching for CRM 4.0 at SDK at http://msdn2.microsoft.com/en-us/default.aspx.

-Download the CRMSdk4.exe file, save it, and execute it to choose the folder where you want to decompress the files.

-To create a Plug-In, you must create a new Class Library project in Visual Studio 2005 by going to the File Menu, New, Project. Then select Visual C#/Windows in the project types on the left and Class Library on the right. Enter a name for the project and select the location and a Solution Name
(The Purpose of this is when we create the New account in CRM the  Description Field in Account Page is generated through our class Library code after Creating the New Account.)


Now Look at the Extracted Files where You set up  CRMSdk4.exe.In that Find Microsoft.Crm.Sdk.dll file to your project as well as add another reference to Microsoft.Crm.SdkTypeProxy.dll. These DLL files are located inside the SDK\Bin subfolder

After you create the project, you must add a reference to the Microsoft.Crm.Sdk.dll file to your project as well as add another reference to Microsoft.Crm.SdkTypeProxy.dll. These DLL files are located inside the SDK\Bin subfolder. To add these references follow these steps:

1. Go to the Solution Explorer and select the Solution name (the root node of the tree), right-click, and select the Add Reference menu option (see Figure).



2.Move to the Browse tab 

3.Locate the SDK\Bin directory on your local drive and select both Microsoft.Crm.Sdk.dll and Microsoft.Crm.SdkTypeProxy.dll files. (To select more than one file hold the Ctrl key.)

4.Click OK to add the references to your project. You see these files inside the References folder of your project in the Solution Explorer (see Figure )


As with any other interface, you must explicitly implement methods. In the case of the IPlugin interface, you must implement the Execute method, as shown in the following code:
namespace myPlugIn
{
  public class Class1 :IPlugin
  {
    #region IPlugin Members
    public void Execute(IPluginExecutionContext context)
    {
      throw new Exception("The method or operation is not implemented.");
    }
    #endregion
  }

}

The previous code implements the method but it only acts to throw an exception. We update the Account description field with the unique identifier value that is assigned when the Account is created to illustrate a more useful example.

To perform this sample, delete the line that is inside the Execute method:
throw new Exception("The method or operation is not implemented.");

and enter the following code instead:
        DynamicEntity entity =
(DynamicEntity)context.InputParameters.Properties["Target"];
        if (entity.Name == EntityName.account.ToString())
        {
            Guid id = (Guid)context.OutputParameters.Properties["Id"];
            Key myKey = new Key(id);

            if (!entity.Properties.Contains("accountid"))
            {
            entity.Properties["accountid"] = myKey;
            }
            if (!entity.Properties.Contains("description"))
            {
            entity.Properties["description"] = "GUID = " + id.ToString();
            ICrmService service = context.CreateCrmService(true);
            service.Update(entity);
            }
        }
- Now Debug the class Library and copy the dll of it and put it some Place.
   Now You have to Register the Plug In

- Download the Plugin Registration tool. (In that download    PluginRegistration.exe(http://archive.msdn.microsoft.com/crmplugin/Release/ProjectReleases.aspx?ReleaseId=2010) )

- Run this and Fill Server,Port,Domain,UserName and Try to connect  then it will ask for   password and   insert the password and then Press OK and then select organization and click on connect
   Now click on Register New Assembly.



-Click on Browse and Specify the Assembly(dll) and select database radio button and click on Register Selected Plugins.

-Now Locate the Plug-In you just registered in the Steps registered and You will see (Plugin) CRMPlugIn.Class1

-Expand the assembly and you will see the Plug Ins with its class Name.and select that PlugIns.  
Now Right Click On it and select Register New Step.

-The Dialog box will shown as below.

-Now Write Create in general Configuration Information Message.

-Write Account in Primary Entity and None to secondary Entity.and then you have to select Execution mode and Eventing Pipeline stage of Execution.and Press Register New Step.

-And Now Go to the CRM Site and create a Account and then Open Newly created Account check description Field You will see "Guid + some id(Guid) that generated "


Plug-In Debugging

There are two ways to debug your Plug-In. The first is attaching the debugger to the w3p.exe process, and the other is forcing the add-in to call the debugger. For either of these methods, it is recommended to try them on a testing environment as these methods use Visual Studio and will interrupt users jobs if attempted on a production environment.

Before trying any of these methods, you need to build your Plug-In with Debug mode and put the PDB (project data base) file generated by the compiler in the following directory (assuming you have the CRM server installed on C:\Program Files\Microsoft Dynamics CRM\):

C:\Program Files\Microsoft Dynamics CRM\Server\bin\assembly


Unfortunately, you must restart the IIS after copying the PDB file. This is something that can be done via the command prompt by running the following command at a command line prompt:

Iisreset


Note

You must copy the PDB file every time you rebuild the solution and restart the IIS.



Attaching the Debugger to the w3wp.exe Process
With this mode, you have to open your Plug-In solution in Visual Studio 2005 first and put in breakpoints where you want to stop and debug your code. To put a breakpoint, press F9 or go to the Debut menu and select the Toggle Breakpoint option

You must start the debugger by attaching the Visual Studio debugger to the w3wp.exe process. To do that, go to the Debug menu and select the Attach to Process option


When the Attach to Process dialog box appears, be sure to check the check boxes that say Show Processes from All Users and Show Processes in All Sessions. Locate the w3wp.exe process, and select it

Note

If you don't find the w3wp.exe process, it is probably because you must first open an Internet Explorer window and browse to your CRM organization URL, so the IIS will load the application pool represented by the w3wp.exe. You might also see more than one instance of this process depending on the application pools configured in the server. To be sure you select all the instances you find, hold the CTRL key before pressing the Attach button.

Press the Attach button to start debugging the Plug-In.

Assuming you registered a Plug-In with a step associated to the Account entity on the Create event (as our example did), when you go to the CRM web application and try to create a new Account, you will be automatically switched to the Visual Studio 2005 application after you click Save or Save and Close on the Account Form

When you finish the debugging, you can stop the debugger by going to the Debug menu and clicking either the Stop Debugging or Detach All menu options

No comments:

Post a Comment