Home

Nov 26, 2010

Develop Your First Plugin in CRM 2011

Let’s get started with Simple Plugin in CRM 2011. The plugin will create a followup task on lead creation. So you need to configure it on post create of Plugin. Plugin registration tool is provided with CRM 2011 SDK and it is similar to CRM 4.0

1. Add the class library project

screen1

2. Change the Project Target Framework to 4.0

Screen 3

3. Add microsoft.crm.sdk.proxy.dll & microsoft.xrm.sdk.dll

screen2

How to get Plugin context in CRM 2011:

You need to get it from serviceProvider by passing Plugin Context type

// Get the IPlugin Context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

How to get Organization Service in CRM 2011:

You need to get OrganizationFactory from Service Provider and then from OrganizationFactory you need to get Organization Service.

//get organization service
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

Here is the complete code:

using System;
usingSystem.Collections.Generic;
usingSystem.Text;
usingMicrosoft.Xrm.Sdk;

namespaceFirstPlugin
{
    public classPostCreateLead : IPlugin
  
{
        public voidExecute(IServiceProvider serviceProvider)
        {
            // Get the IPlugin Context
          
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            if(context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] isEntity)
            {
                // obtain target entity from context
              
Entityentity = context.InputParameters["Target"] asEntity;
                //check if the entity name is lead
              
if(entity.LogicalName != "lead")
                {
                    return;
                }

                //Create a follow up task on lead creation using late binding (entity)
              
Entityfollowup = newEntity();
                followup.LogicalName = "task";
                followup.Attributes = newAttributeCollection();
                followup.Attributes.Add("subject", "created via plugin");
                followup.Attributes.Add("description", "Added Test Description");
                followup.Attributes.Add("scheduledstart", DateTime.Now.AddDays(3));
                followup.Attributes.Add("scheduledend", DateTime.Now.AddDays(5));
                // Refer to the account in the task activity.
               
if (context.OutputParameters.Contains("id"))
                {
                    Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                    string regardingobjectidType = "lead";

                    followup["regardingobjectid"] = new EntityReference(regardingobjectidType, regardingobjectid);
                }
                //get organization service
               
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                service.Create(followup);
            }
        }
    }
}

You can download the firstPlugin here
               

13 comments:

Johan said...

I have created a plugin registration command line tool for Microsoft Dynamics CRM 2011 here: http://pluginregcrm2011.codeplex.com/

abdul sharique said...

I followed the steps for first plugin but getting error follow up entity is not available in metadata
I changed the code little bit

entity followup=new entity();
followup.logicalname="followup"

instead of these lines i used

entity followup=new entity("task");

and removed below line
followup.logicalname="followup"

then its working fine

Amol Gholap said...

Thanks, it is corrected now.

dillon.bailey said...

Another solution for the missing logical name would be to create a custom entity named "followup".

dillon.bailey said...

Another solution for the missing logical name would be to create a custom entity named "followup".

Iain said...

Hi I tried this tutorial.

But I got an error to do with signing the plugin.

Business Process Error
Unexpected exception from plug-in (Execute): Plugin_Test.PostCreateAccount_Plugin: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

The following error log was produced

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): Plugin_Test.PostCreateAccount_Plugin: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.Detail:

-2147220956

Unexpected exception from plug-in (Execute): Plugin_Test.PostCreateAccount_Plugin: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
2013-06-06T09:02:55.3199199Z



[Plugin Test: Plugin_Test.PostCreateAccount_Plugin]
[22facb69-87ce-e211-8a3d-3c4a92dbdc19: Plugin_Test.PostCreateAccount_Plugin: Create of account]




Could you help me understand what needs to be done signing the plugin?

Regards,

Iain

Shobhika Jain said...

I am getting an error for Entity entity = context.InputParameters["Target"] as Entity;
embedded statement cannot be a declaration or labeled statement

Amol Gholap said...

@lain - Do you signed you assembly?

Amol Gholap said...

@Shobika - code looks good to me. Can you please paste your code here?

Sati Raji said...

Hi Amol, Thanks for such a good post. Can yoy pls let me know how can we debug the plugin code using Visual Studio?

Thanks,
Saritha

Sati Raji said...

@ Saritha -- Hi Amol..Thanks for that great post. Is there any way that we can debug the plug in code using Visual Studio?

Amol Gholap said...

check this https://www.youtube.com/watch?v=Xr0Cb-GZssY

vamsinath reddy said...

Hi Amol, thanks for the detail steps.

i am developing the plugin for the first time by following your video.

i copy and pasted exact code into my plugin and registered it successfully. But i am unable to see "Task" activity created for post creation of Account record.

Is there any error in my code or any registration step went wrong???

Could you please help me on this???

Thanks in advance!!!!
Vamsi