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

4 comments:

  1. Simple and straight forward approach.
    Works Perfect.

    I tried this approach with Queue hiding scenario, works well and good.

    Thanks Andriy for the Excellent post.

    Regards
    Vinoth

    ReplyDelete
  2. For my shame - I was not first who developed such plugin. http://crmqueuecounter.codeplex.com/

    ReplyDelete
  3. Still, you were able to learn from it, so life is good, right? :O)

    ReplyDelete
  4. Hello, Jaag. The life is definitely good =)

    ReplyDelete