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.

No comments:

Post a Comment