Take the 2-minute tour ×

I'm trying to retrieve a list of entities from CRM, but I'd like to get each one with the related entities. So far, I've the following code:

FilterExpression filterExpression = new FilterExpression();
ConditionExpression condition = new ConditionExpression(Constants.ModifiedOnAttribute, ConditionOperator.GreaterEqual, lastSync);

QueryExpression query = new QueryExpression()
     EntityName = entityName,
     ColumnSet = new ColumnSet(attributesMetadata.Select(att => att.Name).ToArray<string>()),
     Criteria = filterExpression,
     Distinct = false,
     NoLock = true

RetrieveMultipleRequest multipleRequest = new RetrieveMultipleRequest();
multipleRequest.Query = queryExpression;

RetrieveMultipleResponse response = (RetrieveMultipleResponse)proxy.Execute(multipleRequest);

In the variable response, I can see the EntityCollection attribute, but inside, Related entities always come empty.

Related entities count is 0

I'd like to know if it is possible to retrieve the set of a given entities, with the related entities, using RetrieveMultipleRequest, rather than go one by one using RetrieveRequest.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

One approach to retreive related entities data - adding LinkEntities to your query. Example below will make you an idea how to make this:

LinkEntity linkEntity = new LinkEntity("email", "new_emails", "activityid", "new_relatedemail", JoinOperator.Inner);
linkEntity.EntityAlias = "related";

query = new QueryExpression("email");
query.Criteria.AddCondition("modifiedon", ConditionOperator.NotNull);

And then you can access attributes from related entities using EntityAlias you specified above:

foreach (Entity entity in entities.Entities)
    if ((long)(entity["related.versionnumber"] as AliasedValue).Value > 0)
        stop = false;
share|improve this answer
Thanks SergeyS. That's not the best that I hope, but is what CRM offers :). If I have a list of 100 accounts with 5 related entities, with 3 records each one, I'll get 24.300 records from the server :S –  Milton Dec 5 '12 at 16:59
add comment

The RetrieveMultipleRequest is for returning multiple instances of a particular type of entity. I have spent a year using the CRM SDK from C# and I have found no way of populating those related entity collections in a single query. This basically leaves you with two options:

  1. Use the AliasedValue as SergeyS recommends. Remember when querying 1:Many relationships, be aware that you could be returning multiple results for the same parent entity. This is what I use most of the time.

  2. Perform a second query for each relationship you want access to. You'll probably get better performance if you can use an IN statement in your second query, based on the results of the first, rather than performing a separate query for each result of the first.

Below is some pseudo code to show the difference.

var contacts = GetContacts();

// One Request to get the cars for the contacts
var cars = GetCarsWhereContactIdIn(contacts.Select( c => c.new_ContactId));

foreach(var c in contacts){
    c.new_Cars.AddRange(cars.where(car => car.new_contactId = c.ContactId));

// Verses
var contacts = GetContacts();

foreach(var c in contacts){
    // One Request for each contact
share|improve this answer
Thanks Daryl. I think that some times, the second is the best approach. Anyway, I hope CRM guys give it as related entities in the future :) –  Milton Dec 5 '12 at 17:00
add comment

Your Answer


