Dan
Dan

Reputation: 21

Retrieve all child/descendant/related entities programmatically

How do I retrieve all of the related entities of a source entity in CRM Dynamics 2011, in C#?

thanks

Upvotes: 0

Views: 3602

Answers (1)

Nicknow
Nicknow

Reputation: 7224

FetchXml is overkill for this requirement, but obviously if you want to construct the FetchXml and use it for your QueryBase in place of the QueryExpression I'm showing you are free to do that. The logic remains the same.

//Assumes you have a Entity() object of the parent entity
//somehow you have to know the parent entity record's Id

Guid parentId = parentEntity.Id;
var query = new QueryExpression("new_childentity");
query.Criteria.AddCondition(new ConditionExpression("new_lookupfield", ConditionOperator.Equal, parentId));
query.ColumnSet = new ColumnSet(true);
var results = service.RetrieveMultiple(query);
if (results.Entities.Any())
{
     //Do your processing here
}
else
{
     //Do whatever when there are no child entities
}

Upvotes: 1

Related Questions