Last year I blogged about this on CRM 4.0 but since last few months lots of users queried about similar solution for CRM 2011. This blog entry hopefully will provide solution for this.
Well solution logic is same as CRM 4.0, we will create custom workflow which will take email id and source queue id as input parameter and then custom will removed this email from relevant queue.
I have done following are steps to achieve this in CRM 2011.
Step 1: Create new VS studio project and add necessary reference (i.e. Microsoft.Xrm.Sdk , Microsoft.Xrm.Sdk.Workflow etc.). for more information on how to create custom workflow activity for 2011 refer following article http://technet.microsoft.com/en-us/library/gg328515.aspx
Step 2 : Add new class called RemoveEmail.cs and I have provided full source code for this class at end of this article. (or it can be downloaded from http://snipt.org/xoGi)
Step 3 : Register custom workflow assembly on 2011 dynamics server using Plug in Registration Tool. for more information on how to register custom workflow activity for 2011 refer following article http://technet.microsoft.com/en-us/library/gg328515.aspx
Step 4: following screen print of on demand workflow I have created for this in CRM 2011 for testing purpose.
That’s it, Done. if you need more information or got any more queries regarding this please drop comment below. Hope this helps..
Cheers, MayankP:)
Source Code
using System; using System.Activities; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Discovery; using Microsoft.Xrm.Sdk.Client; namespace CRM2011WorkflowUtilities { public class RemoveEmail : CodeActivity { protected override void Execute(CodeActivityContext executionContext) { try { //Create the context and tracing service IExecutionContext context = executionContext.GetExtension(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); ITracingService tracer = executionContext.GetExtension(); EntityReference incomingEmail = activityId.Get(executionContext); string emailID = incomingEmail.Id.ToString(); EntityReference incomingQueue = queueId.Get(executionContext); string queueID = incomingQueue.Id.ToString(); //Get the QueueItemID Guid queueItemId = GetQueueItem(service,emailID,queueID); //delete relevant queue item record and it will remmoved email from queue if (queueItemId != Guid.Empty) { service.Delete("queueitem", queueItemId); } } catch (Exception ex) { Helpers.Throw(String.Format("An error occurred in the {0} plug-in.", this.GetType().ToString()), ex); } return; } /// /// Retrieve Queue Item for this email and queue /// /// /// /// /// private Guid GetQueueItem(IOrganizationService service,string emailGUID,string QueueGUID) { Guid queueItemId = Guid.Empty; // Share the same access rights as the user, so we must retrieve the user's access rights to the record QueryExpression qe = new QueryExpression(); qe.EntityName = "queueitem"; qe.ColumnSet = new ColumnSet(); qe.ColumnSet.AllColumns = true; FilterExpression filter = new FilterExpression(); ConditionExpression hasEmailId = new ConditionExpression(); hasEmailId.AttributeName = "objectid"; hasEmailId.Operator = ConditionOperator.Equal; hasEmailId.Values.Add(emailGUID) ; ConditionExpression hasQueueId = new ConditionExpression(); hasQueueId.AttributeName = "queueid"; hasQueueId.Operator = ConditionOperator.Equal; hasQueueId.Values.Add(QueueGUID); filter.Conditions.Add(hasEmailId); filter.Conditions.Add(hasQueueId); qe.Criteria = filter; EntityCollection ec = service.RetrieveMultiple(qe); if (ec != null && ec.Entities.Count > 0) { queueItemId = ec.Entities[0].Id; } return queueItemId; } #region Input Parameters [RequiredArgument] [Input("Source Email Id")] [ReferenceTarget("email")] public InArgument activityId { get; set; } [Input("queue Id")] [ReferenceTarget("queue")] public InArgument queueId { get; set; } #endregion #region Output Parameters #endregion } }
using System; using System.Activities; using Microsoft.Xrm.Sdk.Workflow; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Sdk.Discovery; using Microsoft.Xrm.Sdk.Client;
namespace CRM2011WorkflowUtilities { public class RemoveEmail : CodeActivity { protected override void Execute(CodeActivityContext executionContext) { try { //Create the context and tracing service IExecutionContext context = executionContext.GetExtension(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); ITracingService tracer = executionContext.GetExtension();
EntityReference incomingEmail = activityId.Get(executionContext); string emailID = incomingEmail.Id.ToString();
EntityReference incomingQueue = queueId.Get(executionContext); string queueID = incomingQueue.Id.ToString();
//Get the QueueItemID Guid queueItemId = GetQueueItem(service,emailID,queueID);
//delete relevant queue item record and it will remmoved email from queue if (queueItemId != Guid.Empty) { service.Delete("queueitem", queueItemId); } } catch (Exception ex) { Helpers.Throw(String.Format("An error occurred in the {0} plug-in.", this.GetType().ToString()), ex); }
return; }
/// /// Retrieve Queue Item for this email and queue ///
/// /// /// /// private Guid GetQueueItem(IOrganizationService service,string emailGUID,string QueueGUID) { Guid queueItemId = Guid.Empty;
// Share the same access rights as the user, so we must retrieve the user's access rights to the record QueryExpression qe = new QueryExpression(); qe.EntityName = "queueitem"; qe.ColumnSet = new ColumnSet(); qe.ColumnSet.AllColumns = true;
FilterExpression filter = new FilterExpression();
ConditionExpression hasEmailId = new ConditionExpression(); hasEmailId.AttributeName = "objectid"; hasEmailId.Operator = ConditionOperator.Equal; hasEmailId.Values.Add(emailGUID) ;
ConditionExpression hasQueueId = new ConditionExpression(); hasQueueId.AttributeName = "queueid"; hasQueueId.Operator = ConditionOperator.Equal; hasQueueId.Values.Add(QueueGUID);
filter.Conditions.Add(hasEmailId); filter.Conditions.Add(hasQueueId);
qe.Criteria = filter;
EntityCollection ec = service.RetrieveMultiple(qe);
if (ec != null && ec.Entities.Count > 0) { queueItemId = ec.Entities[0].Id; }
return queueItemId;
}
#region Input Parameters
[RequiredArgument] [Input("Source Email Id")] [ReferenceTarget("email")] public InArgument activityId { get; set; }
[Input("queue Id")] [ReferenceTarget("queue")] public InArgument queueId { get; set; }
#endregion
#region Output Parameters
#endregion } }