Wednesday, September 02, 2009

Supported Record Counter For Microsoft Dynamics CRM 4.0

I've already published code of record counter for Microsoft Dynamics CRM 4.0 in this post. Those record counter worked but it had some issues (lookups, advanced finds, form assistant).

I've upgraded the code and now it works perfectly.

UPD: according to comment of Moti Mendelovich to this thread i've one more time updated the code of the plugin and now it works even count of records in fetch response greater then 5000.


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Win32;
using System.Xml;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using Microsoft.Crm.Sdk.Metadata;

namespace RecordCounter
public class ExecuteHandler : IPlugin

#region IPlugin Members

public void Execute(IPluginExecutionContext context)
if (context.Depth != 1) //To calculate count of pages and records another one fetch will be executed
return;//so to avoid infinite loops i need to check the depth of request - if it more then 2 - return

if (context.MessageName == "Execute" && context.InputParameters.Contains("FetchXml"))
XmlDocument indoc = new XmlDocument();

//Retrieve name of entity to display
string entityName = indoc.SelectSingleNode("//fetch/entity").Attributes["name"].InnerText;

if (entityName == EntityName.savedquery.ToString() ||//To make Advanced Find Work
entityName == EntityName.businessunitnewsarticle.ToString() ||//To make Literature work
entityName == EntityName.resource.ToString() ||//To make Service calendar work
entityName == EntityName.systemuser.ToString() ||//To make Service calendar work
entityName == ||//To make Service calendar work
entityName == EntityName.asyncoperation.ToString())

//Creation of Metadata service - it will be need for retrieving of main attribute of entity
IMetadataService mservice = context.CreateMetadataService(false);

RetrieveEntityRequest request = new RetrieveEntityRequest();
request.RetrieveAsIfPublished = false;
request.LogicalName = entityName;
request.EntityItems = EntityItems.EntityOnly;
string primaryFieldName = ((RetrieveEntityResponse)mservice.Execute(request)).EntityMetadata.PrimaryField;
//CrmService Creation
ICrmService crmService = context.CreateCrmService(true);

//Count of records by page - for calculation of pages count
int pagecount = int.Parse(indoc.DocumentElement.Attributes["count"].InnerText);

//I remove this attributes for retrieve of all records in current view

foreach (XmlNode node in indoc.SelectNodes("//fetch/entity/attribute"))

foreach (XmlNode node in indoc.SelectNodes("//fetch/entity/order"))

foreach (XmlNode node in indoc.SelectNodes("//fetch/entity/link-entity"))
foreach(XmlNode subnode in node.SelectNodes("./attribute"))

XmlAttribute aggrAttr = indoc.CreateAttribute("aggregate");
aggrAttr.Value = "true";

XmlNode field = indoc.CreateNode(XmlNodeType.Element, "attribute", null);

XmlAttribute nameAttr = indoc.CreateAttribute("name");
nameAttr.Value = string.Format("{0}id", (entityName == EntityName.activitypointer.ToString() ? "activity" : entityName));

XmlAttribute aggregateAttr = indoc.CreateAttribute("aggregate");
aggregateAttr.Value = "count";

XmlAttribute aliasAttr = indoc.CreateAttribute("alias");
aliasAttr.Value = "C";


//Xml of full result (without paging)
string fullResult = crmService.Fetch(indoc.OuterXml);

XmlDocument fullResultDocument = new XmlDocument();

//Total record count by fetch
int totalRecordCount = int.Parse(fullResultDocument.SelectSingleNode("//resultset/result/C").InnerText);
int totalPageCount = (totalRecordCount / pagecount) + ((totalRecordCount % pagecount) == 0 ? 0 : 1);

string result = string.Format("Total records = {0}, Total pages = {1}", totalRecordCount, totalPageCount);

//Result XML which is the result shown in Grid
XmlDocument outdoc = new XmlDocument();

//Creation of record which will show totals
XmlNode ResultNodeText = outdoc.CreateNode(XmlNodeType.Element, primaryFieldName, null);
ResultNodeText.InnerText = result;

XmlNode ResultNodeId = outdoc.CreateNode(XmlNodeType.Element, string.Format("{0}id", (entityName == EntityName.activitypointer.ToString() ? "activity" : entityName)), null);
ResultNodeId.InnerText = Guid.Empty.ToString();

XmlNode res = outdoc.CreateNode(XmlNodeType.Element, "result", null);

XmlNode ResultNodeType;

//Following code repair icon for record counter icon
if (entityName == EntityName.activitypointer.ToString())
ResultNodeType = outdoc.CreateNode(XmlNodeType.Element, "activitytypecode", null);
ResultNodeType.InnerText = "4212";

//This code repair report view
if (entityName ==
ResultNodeType = outdoc.CreateNode(XmlNodeType.Element, "reporttypecode", null);
ResultNodeType.InnerText = "1";

//Adding record with label of count of pages and records as a first record in recordset
outdoc.SelectSingleNode("//resultset").InsertBefore(res, outdoc.SelectSingleNode("//resultset").FirstChild);
context.OutputParameters["FetchXmlResult"] = outdoc.OuterXml;



Registration of the step for this plugin:

And screenshots:

Advanced find:


Public and private views:

Quick find:

Form assistant:

Auto resolve lookup view:

Source code you can download here.


  1. Hi A33ik,

    i tried your code, and it really works great in Accounts form and Account related advanced find and all...but it is showing an empty record instaed of total number of records and pages for other entities like Contact,Activity,Leads etc.Can you tell me where i am wrong

  2. I see. Try to add primary field of to the views (for contact this is full name field, for activity - subject, e.t.c.) One of the features of this counter )

  3. yes a33ik,

    it worked now ..

    thanks :-).

  4. Hi a33ik,

    Thank you very much for this record counter it works perfectly!

    I am learning a lot about plugins from this.

    Please keep up the good work!

  5. Record count is showing in first row of grid. But I want to show it right near 1 of 50 Seleted, below the grid

  6. This can be done only with unsupported customization. You can buy such counter here -

  7. Hi,
    When i try to register the dll,a error message occured.

    Unhandled Exception: System.Web.Services.Protocols.SoapException: Server was unable to process request.
    PluginRegistrationTool.PluginRegistrationForm.btnRegister_Click(Object sender, EventArgs e)

  8. This is strange. Have you build your own project or used mine?

  9. Hi A33IK,

    its quiet strange taht when i tried your updated code i am getting an error message i all my crm pages.!! i used the same code given here and registered it the same way you have shown here! Can you please tell me what can be the reason...

  10. Have you strongnamed your library?

  11. Andriy, thank you for excellent sample!

  12. Great code, can it be disabled from appearing in the Auto resolve lookup view?

  13. I haven't found how to do it but you can try.

  14. I registered the dll. See screen

    But I see no line with count or anything!

    Any ideas?

  15. You have to register step as shown here -

  16. Andriy - First off, this is a fantastic plugin! It was simple to register and get working. Though I think I found an odd bug. After registering, my users all had issues using our mail merge templates. I am thinking that the mail merge is reading that record you are using to display the count.

    After unregistering it, they were able to perform the MM so I'm think it is that. Do you have any advice as to how I could work around that?

  17. Hi, Mayhem. I've checked mail merge and definitely my plugin breaks it... Add lines

    if (indoc.DocumentElement.Attributes["count"] == null) return;

    after XML load, rebuild plugin, reregister it. This will fix mail merge.

  18. Andriy,

    Awesome code - very neat functionality. We ran into an issue with certain srs reports after deploying this plugin. Anyone experience that?

  19. Could you send me details of those errors at

  20. The hands of health. For large a gap to ensure Crm

  21. Cool plugin. I have a small problem though...

    As soon as I add the new step for this plugin, some queries to the CRM web service break - 'Server is unable to process request'.

    It only seems to affect FetchXML queries - QueryExpression queries work fine.

    Any ideas ?

  22. Hi Andriy,

    seems to be excatly what i need, but i cant get this plugin to work.

    After registration i get the error: "FileNotFoundException. Could not load the File or assembly RecordCounter..."

    Then, after copying the dll manually to the GAC, the error turns into: System.Net.WebException: The underlying connection was closed: could not establish trust relationship for the SSL/TLS secure channel.

    Do you know how to solve this problem?

    Thanks in advance,

  23. >>PC
    You can redevelop this plugin a little bit to make Fetch XML queries to work. Add following lines at the start of plugin's code:

    if (context.CallerOrigin == CallerOrigin.AsyncService ||
    context.CallerOrigin == CallerOrigin.WebServiceApi)

  24. >>Christian

    Strange error... I haven't seen such behavior in all of my developed plugins...

  25. > You can redevelop this plugin a little bit to make Fetch XML queries to work....

    Excellent. Thanks a lot.

  26. This comment has been removed by the author.

  27. Hi Andriy,

    great plugin. i have a small problem...

    i have registered this plug-in but now when i m trying to modify or edit the notes on any lead, it shows error.
    Please help me out.

    Thanks in advance.

  28. For some reason, when I use this code, my grids return with no records at all. any ideas

  29. Could you make screenshots of plugin and steps registration?

  30. unfortunately no, because the system is on a closed and classified network. However, I double and triple checked the registration and it looks fine.

  31. I'm afraid that I'm unable to help you without all required information...

  32. I think I found the issue. I tried using the Linq to XML objects instead of the old xmlDocument and when I switched back to your version with the xmlDocument it worked.
    So it's either something I did wrong, or another issue related to language (i am using hebrew)



  33. I figured my problem - When I used the linq object (XElement instead of xmlDocument), i needed to pass to the output parameter XElement.ToString(SaveOptions.DisableFormatting). It was the formatting that messed it up.

  34. I found another problem with the plug-in. It will not work correctly if the primary attribute for the entity is not shown in the view.
    I found a solution to this - get the list of attributes from the metadata service and then find a string attribute in the metadata that exists in the fetchXml and write the result to that attribute.
    However, This causes a serious drop in performance as the retrieve action on the metadata is a lot heavier. Any ideas on how to solve this issue?



  35. I thought of another solution although this one requires some unsupported parts.

    Instead of writing a "fake" row, I use the following code:
    HttpContext httpContext = HttpContext.Current;
    if (httpContext != null)
    httpContext.Response.Write("var totalRowsAndPages = '" + result + "'") [in the Write method, the text is surrounded by html script tags, but I can't write them here].

    What this code does is add a javascript variable that holds the result of the record counter.

    I then add some javascript code when the grid page loads, that takes this value and adds it to the grids status bar, next to the 'selected X of Y' text.

    This works great when the grid page first loads, but when I press the green refresh button, or delete a record (also causes the grid to refresh), the system throws an error, and the grid empties.

    any thoughts on this option? if I can make it work, it would be great for performance and for grids that don't show the primary attribute of an entity.

  36. Hi! I made some fix in BasePlugin.cs - you should add CampaignActivity as activity, because plugin fails for CampaignActivity

    protected List activities =
    new List(new string[]
    { EntityName.activitypointer.ToString(),