The code of the plugin:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using System.Xml;
namespace QueueHelper
{
public class QHelper : IPlugin
{
#region IPlugin Members
public void Execute(IPluginExecutionContext context)
{
if (context.MessageName == MessageName.RetrieveMultiple &&
context.PrimaryEntityName == EntityName.queue.ToString())
{
BusinessEntityCollection queues = (BusinessEntityCollection)context.OutputParameters[ParameterName.BusinessEntityCollection];
ICrmService crmservice = context.CreateCrmService(true);
for (int i = 0; i < queues.BusinessEntities.Count; i++)
{
DynamicEntity _queue = (DynamicEntity)queues.BusinessEntities[i];
if (!_queue.Properties.Contains("name"))
return;
Guid queueid = ((Key)_queue["queueid"]).Value;
string fetchrequest = string.Format(
@"<fetch mapping='logical' aggregate='true'>
<entity name='queueitem'>
<attribute name='queueitemid' alias='c' aggregate='count'/>
<filter type='and'>
<condition attribute='objecttypecode' operator='ne' value='4406'/>
<condition attribute='queueid' operator='eq' value='{0}'/>
</filter>
</entity>
</fetch>", queueid.ToString());
string fetchresult = crmservice.Fetch(fetchrequest);
XmlDocument document = new XmlDocument();
document.LoadXml(fetchresult);
_queue["name"] = string.Format("{0} - total length - {1} items", _queue["name"], document.SelectSingleNode("//resultset/result/c").InnerText);
queues.BusinessEntities[i] = _queue;
}
context.OutputParameters[ParameterName.BusinessEntityCollection] = queues;
}
}
#endregion members
}
}
The registration of plugin:
And the result of work:
Here you can download source code and built assembly:
Simple and straight forward approach.
ReplyDeleteWorks Perfect.
I tried this approach with Queue hiding scenario, works well and good.
Thanks Andriy for the Excellent post.
Regards
Vinoth
For my shame - I was not first who developed such plugin. http://crmqueuecounter.codeplex.com/
ReplyDeleteStill, you were able to learn from it, so life is good, right? :O)
ReplyDeleteHello, Jaag. The life is definitely good =)
ReplyDelete