Tip #796: Long live ExecuteFetchRequest!

Love those days when people send their gems and discoveries to jar@crmtipoftheday.com – other people’s cleverness tend to rub off on us, making us a bit smarter every single post.

Aron Fischman from xRM Edge LLC made this intricate discovery while working on a product that calculates aggregates on CRM data. In his travels he’s noticed that with ExecuteFetchRequest we can run aggregates on most entities, but with RetrieveMultiple, certain entities are excluded.

For example, the postrole entity does not support RetrieveMultiple, but we can still run aggregates against it using ExecuteFetchRequest. The behavior is the same using CrmServiceClient.

Here is some sample code:

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;

namespace Lab_ExecuteFetchVsRetrieveMultiple
{
  class Program
  {
    static void Main(string[] args)
    {
      var fetch = @"<fetch aggregate='true' mapping='logical'>
                <entity name='postrole'>
                <attribute name='postroleid' alias='aggregate_result' aggregate='count' />
                </entity>
              </fetch>";
      var connectionString = "Url=https://foobar.crm.dynamics.com; Username=user@foobar.onmicrosoft.com; Password=myPass; AuthType=Office365";
      var connection = CrmConnection.Parse(connectionString);
      var executeFetchReq = new ExecuteFetchRequest { FetchXml = fetch };

      //Try with IOrganizationService
      var orgService = new OrganizationService(connection);
      //Works
      var orgSvcExecuteFetchResponse = (ExecuteFetchResponse)orgService.Execute(executeFetchReq);
      //Doesn't work
      var orgSvcRetrieveMultipleResponse = orgService.RetrieveMultiple(new FetchExpression(fetch));

      //Try with CrmServiceClient:
      var crmSvcClient = new CrmServiceClient(connectionString);
      //Works
      var crmSvcExecuteFetchResponse = crmSvcClient.Execute(executeFetchReq);
      //Doesn't work
      var crmSvcRetrieveMultipleResponse = crmSvcClient.RetrieveMultiple(new FetchExpression(fetch));
    }
  }
}

The ExecuteFetchRequest works fine, but the RetrieveMultiple throws an exception:
Long live fetch

When pressed, Aron dug out the whole list of entities with the same behavior.

  • AuthorizationServer
  • BusinessDataLocalizedLabel
  • BusinessProcessFlowInstance
  • Calendar
  • CalendarRule
  • ChildIncidentCount
  • ComplexControl
  • DependencyFeature
  • ImageDescriptor
  • LookUpMapping
  • MailboxStatistics
  • MetadataDifference
  • MultiEntitySearch
  • MultiEntitySearchEntities
  • OrganizationUI
  • PartnerApplication
  • PostRegarding
  • PrincipalAttributeAccessMap
  • PrincipalEntityMap
  • PrincipalObjectAccessReadSnapshot
  • PrincipalSyncAttributeMap
  • QueueItemCount
  • QueueMemberCount
  • RecordCountSnapshot
  • RollupJob
  • RollupProperties
  • SharePointDocument
  • SqlEncryptionAudit
  • Subscription
  • SubscriptionClients
  • SubscriptionManuallyTrackedObject
  • SubscriptionSyncInfo
  • SyncAttributeMapping
  • SyncAttributeMappingProfile
  • SystemApplicationMetadata
  • SystemForm
  • SystemUserManagerMap
  • SystemUserSyncMappingProfiles
  • TeamSyncAttributeMappingProfiles
  • TimeZoneLocalizedName
  • TraceAssociation
  • TraceRegarding
  • UserApplicationMetadata
  • WorkflowWaitSubscription
None of these strike him as “major” entities that users should be concerned with, but he could see an Admin possibly wanting to count some of these entities. And, it is interesting that deprecating ExecuteFetchRequest removes some functionality – however trivial it may be.

Leave a Reply

Your email address will not be published. Required fields are marked *