[CRM 4] AggregateOf property in entity attributes – Using CRM Metadata Service

Have you tried sometimes retrieve the value of “AccountId” property from Contact entity? Or, have you tried retrieve the value of “OwningUser” from Contact entity? Which were the results? Nothing, weren’t they?. Don’t worry, your CRM works fine, you didn’t miss anything. The reason for this is found in “AggregateOf” property in entity attributes.

The point is the following:

Any attribute, whose property “AggregateOf” is NOT NULL, doesn’t return any value from CRM service requests

Which are these attributes? :

 

What does “AggregateOf” exactly mean? :

An attribute whose “AggregateOf” property in NOT NULL means that its value is part of other bigger aggregation expression in other attribute. For example, take accountid and parentcontactid. The value of their "AggreationOf” property is “parentcustomerid” for both. So, if we go to CRM database, in “Contact” view, we will see this:

ParentCustomerId   = coalesce(ContactBase.AccountId, ContactBase.ParentContactId)

In other way:

ParentCustomerId   =

case

when ContactBase.AccountId IS NOT NULL then ContactBase.AccountId

when ContactBase.ParentContactId IS NOT NULL then ContactBase.ParentContactId

else NULL

end

 

Why do I not retrieve any value from CRM Service?:

Because of these “special” attributes are part of other bigger aggregation expression, you should use that resulting attribute. In our previous example with Contact entity, you should retrieve “parentcustomerid” instead of “Accountid” or “ParentContactId”.

How can I know when I will have this “problem” and how can I resolve it?: You have two options:

  • Check if attribute that you want to retrieve is in that previous list  [I don’t like it, but It’s quick]
  • Use CRM Metadata Service to check “AggreateOf” property [It’s less efficient, but it’s more flexible]. You can use this code to check it:
        public string AggregateOfAttribute(string attributeName, string entityName)
        {
            RetrieveAttributeRequest request = new RetrieveAttributeRequest();
            request.EntityLogicalName = entityName;
            request.LogicalName = attributeName;
            request.RetrieveAsIfPublished = true;
 
            MetadataService metadataService = new MetadataService();
            //Set Metadata Service Properties (Url, Credentials, CrmAuthenticationTokenValue)
            RetrieveAttributeResponse response = (RetrieveAttributeResponse)metadataService.Execute(request);
            AttributeMetadata attributeMetadata = response.AttributeMetadata;
            return attributeMetadata.AggregateOf;
 
        }

About Ramon Tebar

Software Engineer specialised on Microsoft Technologies with experience in large projects for different industrial sectors as developer, consultant and architect. I enjoy designing and developing software applications, it is my job and one of my hobbies. I’m interested in design patterns, new technologies and best practices. Making those part of the ALM process is a great challenge. During the last years, I have specialised in Microsoft Dynamics CRM (now Dynamics 365). I customise and extend the platform to provide tailored solutions and integrations based on service-oriented architectures and messages queuing. Motivated by community events and contributor in blogs, technical books, open source projects and forums, I have been awarded Microsoft Most Valuable Professional (MVP) on Dynamics 365 (CRM) since 2012.

Posted on September 23, 2009, in CRM. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: