Saturday, January 24, 2009

CRM SDK Custom Plugin Helper Class

This is the Plugin Helper that I wrote in C# for Microsoft Dynamics CRM 4.0.
First you need to import it into the namespace of your current project.
Then in your main code create a new instance of the class.
Then you need to set the service property of the class.
Now you are free to use the methods of the class.

This is my Helper class



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.Sdk.Metadata;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using System.Web.Services;


public class Helper
{
public ICrmService service;
public Helper(ICrmService iService)
{
service = iService;
}
public String GetEntityId(IPluginExecutionContext context)
{
String entityId = "";
switch (context.MessageName)
{
case "Create":
if (context.OutputParameters.Contains(ParameterName.Id))
{
entityId = (String)context.OutputParameters.Properties[ParameterName.Id];
}
break;
case "Update":
if (context.InputParameters.Contains(ParameterName.Target) &&
context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
{
IMetadataService metadataService = context.CreateMetadataService(false);
RetrieveEntityRequest contextRequest = new RetrieveEntityRequest();
contextRequest.LogicalName = context.PrimaryEntityName;
contextRequest.EntityItems = EntityItems.IncludeAttributes;
RetrieveEntityResponse contextResponse = (RetrieveEntityResponse)metadataService.Execute(contextRequest);
string keyName = contextResponse.EntityMetadata.PrimaryKey;
DynamicEntity Uentity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];
entityId = ((Key)Uentity.Properties[keyName]).Value.ToString();
}
break;
case "Delete":
case "Assign":
if (context.InputParameters.Properties.Contains(ParameterName.Target))
{
Moniker DAentity = (Moniker)context.InputParameters.Properties[ParameterName.Target];
entityId = DAentity.Id.ToString();
}
break;
case "SetState":
case "SetStateDynamicEntity":
Moniker SetSEntity = (Moniker)context.InputParameters.Properties[ParameterName.EntityMoniker];
entityId = SetSEntity.Id.ToString();
break;
default:
return entityId;
}
return entityId;
}
public DynamicEntity GetDynamicEntity(String EntityId, String EntityName)
{
TargetRetrieveDynamic dynamictarget = new TargetRetrieveDynamic();
dynamictarget.EntityId = new Guid(EntityId);
dynamictarget.EntityName = EntityName;
RetrieveRequest retrieve = new RetrieveRequest();
retrieve.Target = dynamictarget;
retrieve.ColumnSet = new AllColumns();
retrieve.ReturnDynamicEntities = true;
try
{
RetrieveResponse response = (RetrieveResponse)service.Execute(retrieve);
DynamicEntity entity = (DynamicEntity)response.BusinessEntity;
return entity;
}
catch (System.Web.Services.Protocols.SoapException)
{
return null;
}
}
public BusinessEntityCollection GetBusinessEntityCollection(String TargetEntityName, String Cond1AttributeName, ConditionOperator Cond1Operator, string[] Cond1Values, String Cond2AttributeName, ConditionOperator Cond2Operator, string[] Cond2Values, LogicalOperator FilterLogicalOperator)
{
QueryExpression query = new QueryExpression();
query.EntityName = TargetEntityName;
query.ColumnSet = new AllColumns();
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = Cond1AttributeName;
condition1.Operator = Cond1Operator;
condition1.Values = Cond1Values;
ConditionExpression condition2 = new ConditionExpression();
condition2.AttributeName = Cond2AttributeName;
condition2.Operator = Cond2Operator;
condition2.Values = Cond2Values;
FilterExpression filter = new FilterExpression();
filter.FilterOperator = FilterLogicalOperator;
filter.Conditions.Add(condition1);
filter.Conditions.Add(condition2);
query.Criteria.AddFilter(filter);
RetrieveMultipleRequest retrieveMultRequest = new RetrieveMultipleRequest();
retrieveMultRequest.Query = query;
retrieveMultRequest.ReturnDynamicEntities = true;
try
{
RetrieveMultipleResponse retrieveMultResponse = (RetrieveMultipleResponse)service.Execute(retrieveMultRequest);
BusinessEntityCollection collection = (BusinessEntityCollection)retrieveMultResponse.BusinessEntityCollection;
return collection;
}
catch (System.Web.Services.Protocols.SoapException)
{
return null;
}
}
public BusinessEntityCollection GetBusinessEntityCollection(String TargetEntityName, String Cond1AttributeName, ConditionOperator Cond1Operator, string[] Cond1Values, LogicalOperator FilterLogicalOperator)
{
QueryExpression query = new QueryExpression();
query.EntityName = TargetEntityName;
query.ColumnSet = new AllColumns();

ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = Cond1AttributeName;
condition1.Operator = Cond1Operator;
condition1.Values = Cond1Values;
FilterExpression filter = new FilterExpression();
filter.FilterOperator = FilterLogicalOperator;
filter.Conditions.Add(condition1);
query.Criteria.AddFilter(filter);
RetrieveMultipleRequest retrieveMultRequest = new RetrieveMultipleRequest();
retrieveMultRequest.Query = query;
retrieveMultRequest.ReturnDynamicEntities = true;
try
{
RetrieveMultipleResponse retrieveMultResponse = (RetrieveMultipleResponse)service.Execute(retrieveMultRequest);
BusinessEntityCollection collection = (BusinessEntityCollection)retrieveMultResponse.BusinessEntityCollection;
return collection;
}
catch (System.Web.Services.Protocols.SoapException)
{
return null;
}
}
}



There are other ways to go about each function. They are just the ways I have always used in my plugins and moreover...they work.

Now keep in mind I have not used this plugin helper in practice. Therefore all of it might not work. I will continue testing all of the functionality and keep you updated.

Thursday, January 22, 2009

Create Your Own CRM 4.0 Plugin Helper

Microsoft Dynamics CRM 4.0

Sometimes I get tired of writing the same code over and over again. For example, when I retrieve a Dynamic Entity and set the ColumnSet to AllColumns, its the same thing over and over again.


TargetRetrieveDynamic(EntityName, EntityId)
RetrieveRequest(Target, ColumnSet, ReturnDynamicEntities)
RetrieveResponse(Request)
DynamicEntity = (DynamicEntity)retrievedEntity


Also it is the same thing getting the entityId from the context. Although this does depend on what message, pipeline, and whether it is a pre or post event, the code is always the same.

And how about those messy Query Expressions, Condition Expressions, Filter Expressions, Retrieve Multiple, and so on.

Wouldn't it be nice to build a class that will handle all of those things for you with minimal effort on your part. I have been creating plugins for about a year now and I have by no means mastered the craft as of yet but I believe I have done it enough to know that the CRM SDK is not nice when it comes to carpal tunnel. This is why your poor fingers need a break every now and then. That is why I have created the Ultimate CRM SDK Plugin Helper Class 1.0. I say 1.0 because it is still a work in progress. i.e. look for updates on my blog.


Link to the CRM SDK Plugin Helper Class.