Tuesday, January 24, 2012

Plugin In CRM 2011

      1)      Create a new class library project in Microsoft Visual Studio as shown here. This sample uses “Plug-in” as the project name
Create project in Visual Studio

2        2)      Add the following references from the SDK\bin folder.
  • Microsoft.Xrm.Client.dll
  • Microsoft.Xrm.Sdk.dll
     3) Add the following .NET references.
  • Microsoft.IdentityModel.dll
  • System.Data.Services
  • System.Data.Services.Client
  • System.Runtime.Serialization
  • System.ServiceModel
If you do not have the Microsoft.IdentityModel.dll file, you must install Windows Identity Foundation.

4)  Right-click the project in Visual Studio, clicks Add, and then clicks Existing Item.
5) Select the “xrm.cs” file that you created when you generated the early bound types.
Sign your plug-in project
It must that you have to sign in your plug-in before registering the   plug-in.
Here are the steps for signing a plug-in
Add a strong key to your project
1) Open the properties pane under your plug-in project.
                     Open properties pane
2) Create a new strong key file by clicking the Signing tab, select the Sign the assembly check box and select in the drop down list.
Create strong key

3) Type a name for your strong key (in this example, it is “PluginWalkthrough”) and clear the”Protect my key file with a password” check box before clicking OK.

Create a strong name key

4) Save your changes.

Create the plug-in that will run your code

1) Right-click your project again, clicks Add, and then clicks New Item.
2)  Select Class from the options, type the name “Plugin.cs”, and then click Add.

Create a plugin

3) Add the following code to the Plugins.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Client;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Data.Services;
using System.Data.Services.Client;
using System.Diagnostics;
using Xrm;

namespace Sampleplugin
    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.

                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                    Annotation annotation = new Annotation();
                    annotation.ObjectId.Id = entity.Id;
                    annotation.ObjectId.LogicalName = entity.LogicalName;
                    annotation.ObjectTypeCode = entity.LogicalName;
                    annotation.NoteText = "This added by the plugin ";
                catch (FaultException ex)
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);


4) Build the solution.
5) We can find the  .dll in Debug/bin folder of your project folder

No comments:

Post a Comment