Monday, 24 October 2011

Get CRM 2011 Entity record count

using System;
using System.Collections.Generic;
namespace wa_Parracom_Upload {
   public static class CountRecords {
   public static int Count(Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy Org, string arg_entityName)
   {
      try {
         if (String.IsNullOrEmpty(arg_entityName)) return -1; // Argumet Usage
         string EntityName = arg_entityName.Trim().ToLowerInvariant();
         if (String.IsNullOrEmpty(EntityName)) return -1; // Invalid entity Name

         string PrimaryIdAttribute = "";
         try {
            Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest mrq = new Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest {
               LogicalName = EntityName,
               EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.Entity
            };
            Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse mrp =
               (Microsoft.Xrm.Sdk.Messages.RetrieveEntityResponse)Org.Execute(mrq);
            PrimaryIdAttribute = mrp.EntityMetadata.PrimaryIdAttribute;
         } catch (Exception) { return -1; /* Unknown Entity */ }

         System.Xml.XmlDocument fetch = new System.Xml.XmlDocument();
         {
            System.Xml.XmlNode MyXML_fetch = fetch.CreateNode(System.Xml.XmlNodeType.Element, "fetch", "");
            System.Xml.XmlNode MyXML_entity = fetch.CreateNode(System.Xml.XmlNodeType.Element, "entity", "");
            System.Xml.XmlNode MyXML_attribute = fetch.CreateNode(System.Xml.XmlNodeType.Element, "attribute", "");
            System.Xml.XmlAttribute MyXML_name = fetch.CreateAttribute("name", "");
            MyXML_name.Value = PrimaryIdAttribute;
            MyXML_attribute.Attributes.Append(MyXML_name);
            System.Xml.XmlAttribute MyXML_alias = fetch.CreateAttribute("alias", "");
            MyXML_alias.Value = "count";
            MyXML_attribute.Attributes.Append(MyXML_alias);
            System.Xml.XmlAttribute MyXML_aggregate = fetch.CreateAttribute("aggregate", "");
            MyXML_aggregate.Value = "count";
            MyXML_attribute.Attributes.Append(MyXML_aggregate);
            MyXML_entity.AppendChild(MyXML_attribute);
            System.Xml.XmlAttribute MyXML_name_1 = fetch.CreateAttribute("name", "");
            MyXML_name_1.Value = EntityName;
            MyXML_entity.Attributes.Append(MyXML_name_1);
            MyXML_fetch.AppendChild(MyXML_entity);
            System.Xml.XmlAttribute MyXML_mapping = fetch.CreateAttribute("mapping", "");
            MyXML_mapping.Value = "logical";
            MyXML_fetch.Attributes.Append(MyXML_mapping);
            System.Xml.XmlAttribute MyXML_aggregate_1 = fetch.CreateAttribute("aggregate", "");
            MyXML_aggregate_1.Value = "true";
            MyXML_fetch.Attributes.Append(MyXML_aggregate_1);

            fetch.AppendChild(MyXML_fetch);
         }
         Microsoft.Xrm.Sdk.Query.FetchExpression Fetch = new Microsoft.Xrm.Sdk.Query.FetchExpression(fetch.InnerXml);
         Microsoft.Xrm.Sdk.EntityCollection Ents = Org.RetrieveMultiple(Fetch);
         if (Ents.Entities != null && Ents.Entities.Count >= 1 && (Ents.Entities[0]).Contains("count")) {
            Microsoft.Xrm.Sdk.AliasedValue val = ((Ents.Entities[0])["count"]) as Microsoft.Xrm.Sdk.AliasedValue;
            if (val != null)
               return (int)val.Value;
            }
            return -1; // Unexpected result
         }
         catch (Exception) { return -1; /* Unknown Error */ }
      }
   }
}

No comments:

Post a Comment