What’s new in CRM 2011 plugins ?

I wrote a plugin to cascade changes from account addresses to their child contacts in 2011 then converted it back to v4. Let’s look at some of the differences I found.

CRM v4

public void Execute(IPluginExecutionContext context) {
  DynamicEntity target = (DynamicEntity)context.InputParameters[ParameterName.Target];
    if (target.Name.Equals("account")) {
      if (target.Properties.Contains("accountid")) {

CRM 2011

public void Execute(IServiceProvider serviceProvider) {
  IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext));
  Entity target = (Entity)context.InputParameters["Target"];
    if (target.LogicalName.Equals("account")) {
      if (target.Attributes.Contains("accountid")) {
  • Getting the context and target is slightly more complex.
  • .Properties has become .Attributes.

 

CRM v4

QueryByAttribute query = new QueryByAttribute();
query.EntityName = "account";
query.Attributes = new string[] { "accountid" };
query.Values = new Object[] { accountid };
query.ColumnSet = cols;

CRM 2011

QueryByAttribute query = new QueryByAttribute();
query.EntityName = "account";
query.Attributes.AddRange("accountid");
query.Values.AddRange(accountid);
query.ColumnSet = cols;
  • Queries no longer require string or object arrays

 

CRM v4

ICrmService service = context.CreateCrmService(false);
RetrieveMultipleRequest req = new RetrieveMultipleRequest();
req.Query = query;
req.ReturnDynamicEntities = true;
RetrieveMultipleResponse resp = (RetrieveMultipleResponse)service.Execute(req);
BusinessEntityCollection theAccounts = resp.BusinessEntityCollection;
if (theAccounts.BusinessEntities.Count > 0) {
  DynamicEntity theAccount = (DynamicEntity) theAccounts.BusinessEntities[0];

CRM 2011

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
RetrieveMultipleRequest req = new RetrieveMultipleRequest();
req.Query = query;
RetrieveMultipleResponse resp = (RetrieveMultipleResponse)service.Execute(req);
EntityCollection theAccounts = resp.EntityCollection;
if (theAccounts.Entities.Count > 0) {
  Entity theAccount = theAccounts.Entities[0];
  • Accessing the service is done through a Factory class
  • No longer have to specify that you want dynamic entities
  • BusinessEntities have just become Entities
  • Treat dynamic entities the same as any other entity

 

CRM v4

TargetUpdateDynamic update = new TargetUpdateDynamic();
update.Entity = theContact;
UpdateRequest updatereq = new UpdateRequest();
updatereq.Target = update;
UpdateResponse updateresponse = (UpdateResponse)service.Execute(updatereq);

CRM 2011

UpdateRequest updatereq = new UpdateRequest();
updatereq.Target = theContact;
UpdateResponse updateresponse = (UpdateResponse)service.Execute(updatereq);
  • No longer a need for a TargetUpdateDynamic object

 

CRM v4

Guid accountid = ((Key)target.Properties["accountid"]).Value;
CrmBoolean trigger = (CrmBoolean)theAccount.Properties[accountTrigger];

CRM 2011

Guid accountid = (Guid)target.Attributes["accountid"];
bool trigger = (bool)theAccount.Attributes[accountTrigger];
  • Native C# data types, WOO!

 

Advertisements
Leave a comment

2 Comments

  1. Thank you very much for this information Pete. You just saved me a few days of comparing namespaces to upgrade from CRM 4.0 to 2011. I wish Microsoft would leave the namespaces the same so Developers don’t constantly have to fiddle with the code.

    Pete S.

    Reply
  2. “Whats new in CRM 2011 plugins ? Pete’s CRM 2011 Stuff” was indeed a delightful post, can not help but wait to examine far more of ur articles. Time to squander numerous time on the web lmao. Thanks for the post ,Michel

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: