Saturday, December 24, 2011

Plugin differences in CRM 4.0 and 2011


5 syntax changes in Dynamics CRM 2011 plugins

There are number of changes between Dynamics CRM 2011 SDK and CRM 4 SDK. Let’s take a look at what has changed between plugins.
1. The IPlugin now resides in Microsoft.Xrm.Sdk namespace instead of Microsoft.Crm.Sdk
public class ClassName : Microsoft.Crm.Sdk.IPlugin
public class ClassName : Microsoft.Xrm.Sdk.IPlugin

2. The Execute method signature of the IPlugin interface has changed, it now expects an IServiceProvider
 instead of the IPluginExecutionContext.
public void Execute(IPluginExecutionContext context)
public void Execute(IServiceProvider serviceProvider)
3. To get a reference to the IPluginExecutionContext you now need to call the GetService method of the
 IServiceProvider interface.
public void Execute(IPluginExecutionContext context)
Microsoft.Xrm.Sdk.IPluginExecutionContext context =(Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
4. ICrmService has been changed to IOrganizationService.
ICrmService sdk = context.CreateCrmService(true);
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)
serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService sdk = factory.CreateOrganizationService(context.UserId);
5. DynamicEntity has been changed to Entity.
    ·         Properties property of DynamicEntity no longer exists
    ·         Getting and Setting values no longer use *Property classes,
      eg: no more KeyProperty, StringProperty…etc, simply do int value = (int)entity[“schema_name”];
if (context.InputParameters.Properties.Contains("Target") && 
    context.InputParameters.Properties["Target"is DynamicEntity)
if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"is Entity)

If any query please let me know.

Thanks & Cheers!!!

Monday, December 19, 2011

How to debug workflow assemblies and plug-ins in MSCRM 4.0


Remote debugging sounds like the title to a book but it isn't - in truth, I have never used remote debugging because haven't needed to. In MSCRM 3.0 the exceptions bubbled up nicely in workflow and I could do a lot on my own machine. However in MSCRM 4.0, those exceptions don't bubble up nicely :( All you get is something like this:
Beside's adding logging to your assembly (which you should do anyway), what's a dev to do now? You can now debug your plug-in's and custom workflow activities using the remote debugging facility in Visual Studio and that's what I hope to show in the next 9 easy steps!
  1. On your machine you'll find a folder <system drive>\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Remote Debugger (assuming you installed Visual Studio 2008 to the default location) which contains an x86 folder. Copy that to the MSCRM server and run the msvsmon.exe file.
  2. Now click Tools -> Permissions and give yourself access to it.
  3. Now load you plug-in or custom workflow activity (when I refer to plug-in for the rest of the document it means both, unless otherwise stated) using the *cough*annoying*cough* plug-in registration/developer tool.
  4. Next copy the PDB file for your assembly to the <system drive>\Program Files\Microsoft Dynamics CRM\Server\bin\assembly (assuming you installed MSCRM 4.0 to the default location).
  5. Now to finish up the MSCRM server side do an IISReset and restart the Async Windows Service.
  6. Back on your machine now in Visual Studio, set a break point in your code.
  7. Next click Debug -> Attach to Process. The transport should be Default and the Qualifier should be <domain>\<username running msvsmon.exe on the server>@<Server name>. Note: You can get this from the Tools -> Options in msvsmon.exe. You may need to do some firewall configuration but it should be automated.
  8. Next select the process to attach to, for an online plug-in, attach the debugger to the w3wp.exe process on the Microsoft Dynamics CRM server. For an offline plug-in, attach the debugger to the Microsoft.Crm.Application.Hoster.exe process. For asynchronous registered plug-ins or custom workflow activity attach to the CrmAsyncService.exe process.
  9. Now do whatever is needed to trigger your event and watch Visual Studio on your machine do it's magic!