Sunday, October 31, 2010

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 IServiceProvide

public void Execute(IServiceProvider serviceProvider)

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

IPluginExecutionContext context = (IPluginExecutionContext)

5. ICrmService has been changed to IOrganizationService

IOrganizationServiceFactory serviceFactory =
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


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)

Tuesday, October 26, 2010

Step by step plugin tutorial for CRM 2011

This is a step by step guide to create a plug-in in CRM 2011.

1. Create a class library project in vs2010.
2. Sign the assembly by using the Signing tab of the project's properties sheet.
3. Add references to microsoft.crm.sdk.proxy , Microsoft.xrm.sdk ,
System.ServiceModel and System.Runtime.Serialization.
4. Here is code for this tutorial. This plugin will check if the account number
field is empty, create a task for the user to fill this up.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using System.ServiceModel;

namespace CRMPlugin1
public class createNote:IPlugin
public void Execute(IServiceProvider serviceProvider)

// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)

// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];

//check if the account number exist

if (entity.Attributes.Contains("account number") == false)

//create a task
Entity task = new Entity("task");
task["subject"] = "Account number is missing";
task["regardingobjectid"] = new EntityReference("account",new Guid(context.OutputParameters["id"].ToString()));

//adding attribute using the add function
// task["description"] = "Account number is missng for the following account. Please enter the account number";
task.Attributes.Add("description", "Account number is missng for the following account. Please enter the account number");

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

// Create the task in Microsoft Dynamics CRM.


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


}//end class
}//end name space

5. Compile the code and register it on the registration tool provided with sdk.
6. Register it on the post create event of account entity.
Note :check the sdk for instructions to register a plugin


Monday, October 25, 2010

CRM 2011 Beta - Sharepoint Integration Bug


I am playing around with crm 2011 beta from last few weeks and I found a bug in crm-sharepoint integration. It occurs when you select your document management folder structure based on the entity, in my case I chose "account".

If you create an account with spaces ("my test account"), click on the documents on left navigation bar, the system will prompt you to create a folder (htttp://localhost/account/my test account"). Press yes and system will create a folder "my test account" in sharepoint.

Now create a new opportunity ("opportunity 1") and click on the documents on the left navigation bar. The system will prompt you to create a folder ("htttp://localhost/account/my test account/opportunity/opportuynity 1"). Press yes,
the system will come back with the following message:

and the system will create a folder anyway.

Now if you create another record like opportunity or invoice and click on documents the system will ask you to create the folder like ("htttp://localhost/account/my-test- account/opportunity/opportuynity 3"). Press yes and the system will create a new account folder "my-test- account" and create the "opportunity/opportunity 3" folder.

Every time you create a new folder for any related entity it will work properly and create all the sub folders in "my-test- account". But the first opportunity record still displays the error message. You do have an option to correct the location, buts it’s a bug and you end up with 2 folders, one with spaces and one with "-" s.

Good bye.

Thursday, October 21, 2010

Using XRM SDK in MSCRM 4.0 Plugins

I am back and very excited about MSCRM 5.0. But today I am putting a sample to use xrm sdk in plugins. By default there no method in XrmDataContext class to pass plugin context.
But there are ways around it. You can pass the connection as string or as crmconection as follow

"Authentication Type=Integrated; Server=http://localhost:" +
new Uri(Microsoft.Win32.Registry.LocalMachine.OpenSubKey ("SOFTWARE\\Microsoft\\MSCRM", false).GetValue("ServerUrl").ToString()).Port +
"/" + context.OrganizationName)

This worked for me.

Here is sample code I have used to create a note for an account

public void Execute(IPluginExecutionContext context)
Guid ID = new Guid();

switch (context.MessageName)
case "Create":
ID = new Guid(context.OutputParameters.Properties["id"].ToString());
case "Update":
ID = (Guid)((Microsoft.Crm.Sdk.Key)((Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"]).Properties[context.PrimaryEntityName + "id"]).Value;
case "SetState":
ID = (Guid)((Microsoft.Crm.Sdk.Moniker)context.InputParameters.Properties["EntityMoniker"]).Id;
case "SetStateDynamicEntity":
ID = (Guid)((Microsoft.Crm.Sdk.Moniker)context.InputParameters.Properties["EntityMoniker"]).Id;
case "Delete":
ID = (Guid)((Microsoft.Crm.Sdk.Moniker)context.InputParameters.Properties["Target"]).Id;


var crm = new XRM.MyDataContext(context);

var note = new XRM.annotation()
subject = "My XRM Note",
notetext = "This is my XRM Sample.",
Account_Annotation_id = ID,
objecttypecode = "account",