Wednesday, 19 April 2017

CRM 2011 Plugin Syntax Changes

Software upgrades not only bring enhancements, they do break the existing syntax and cause frustration while upgrading to the new version. MS CRM 2011 is no exception to this, as many changes were applied to the way plugins and workflows are developed. While MS CRM 4.0 has its own syntax, CRM 2011 introduced many more additions and altered the syntax significantly.
While we were upgrading the plug-ins and workflows from MS CRM 4.0 to 2011, these syntactical changes took some time to deal with the upgrade process smoothly. During this process, we’ve noted many such changes and compiled them in as orderly manner as possible.
1) References
We no longer use the Microsoft.Crm.* DLLs, as the namespace had a complete makeover. MS CRM 2011 introduced a brand new namespace “XRM”, hence the DLLs to use are Microsoft.Xrm.*
Since the changes in MS CRM 2011 SDK are using WCF based services (compared to the web services used by its predecessor), we must reference the below mentioned:
o System.ServiceModel
o System.Runtime.Serialization
A reference to the “Microsoft.IdentityModel” DLL is a must if the plugin is targeted at MS CRM 2011 online version.

2) Executing
Execute method now uses IServiceProvider.
CRM 4.0CRM 2011
public void Execute(IPluginExecutionContext context)public void Execute(IServiceProvider serviceProvider)
3) IPluginExecutionContext
Similar to the “Execute” method retrieving the context object inside a plugin also changed:
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
4) CRM Service
In CRM 2011, ICrmService is replaced by IOrganizationService
CRM 4.0CRM 2011
ICrmService _CS = context.CreateCrmService(true);IOrganizationServiceFactory factory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
5) DynamicEntity has been changed to Entity
CRM 4.0CRM 2011
if ((context.InputParameters.Properties.Contains(“Target”) == true) && (context.InputParameters.Properties[“Target”] is DynamicEntity))
{
}
if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is Entity)
{
}
Similarly “Properties” of a CRM entity now have to be accessed using the “Attribute” collection:
entity.Attributes.Contains("new_incidentnumber")
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);
}
}
}
}
}

 Next time, we’ll be listing all the syntax changes made in the custom workflow activities.
Presented By www.Osmosys.asia

No comments:

Post a Comment

Voice of the Customer (VOC) – Dynamics 365 CRM

Tags:   Dynamics 365   Dynamics CRM   feedback   survey   VOC   Voice of customer Introduction To Voice of the Customer For any bus...