Thursday, September 16, 2010

Queues - plugin which shows the count of items inside queue

I developed the plugin to make it possible to see near the name of queue the total count of items inside it. But it doesn't work for Assigned and In-Progress (personal queues).

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"))

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}'/>
</fetch>", queueid.ToString());

string fetchresult = crmservice.Fetch(fetchrequest);
XmlDocument document = new XmlDocument();

_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: