Monday 7 May 2012

Changes in CRM 2011 plugins


There are a lot of syntax and functional changes in MSCRM 2011 plugins. I have listed some of them below:

1. New References
• System.ServiceModel
• System.Runtime.Serialization

2. Microsoft.Crm.Sdk has been replaced by Microsoft.Xrm.Sdk

3. The default parameter for execute method has been changed from
IPluginExecutionContext to IServiceProvider

public void Execute(IServiceProvider serviceProvider)


serviceProvider
A container for service objects. Contains references to the plug-in execution context (IPluginExecutionContext), tracing service (ITracingService), organization service (IOrganizationServiceFactory), and notification service (IServiceEndpointNotificationService).

4. IpluginExecutionContext has been changed to a type of service and to get the reference use the following syntax.

IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));

5. ICrmService has been changed to IOrganizationService

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

6. Dynamic entity has been changed to “Entity”

if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
}


7. Dynamic Entity’s properties syntax has been changed to attributes

entity.Attributes.Contains("accountnumber")

8. Strongly typed classes (account, contact) are missing in Microsoft.XRM.Sdk assembly but there is an exception to this.

9. CRM 2011 provides the crm utility called “crmsvcutil” to generate strongly typed classes that can be used in plugins.

10. Strongly typed classes has been changed to use like loosely typed classes.
Task task = new Task();
Task[“description”] = ”test description”

11. No more soap exception
catch (FaultException ex)


Below given template can be used to create a plugin for the MS CRM 2011

using System;
using System.ServiceModel;
using System.Runtime.Serialization;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

namespace MyPlugin
{
public class MyPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
 IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
 Entity entity = (Entity)context.InputParameters["Target"];
 if (entity.LogicalName != "entity logical name”)
 { return; }
try
{
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
//Plugin Code
 }

catch (FaultException ex)
 { 
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}
}

Enjoy CRM !!!