Home

Dec 7, 2010

CRM 2011 : Retrieve related entity data

I was working on requirement of retrieving users for a particular team. Team & users are N:N related. I might need to right long LinkEntity query to retrieve users for particular team. Guess what, I can do it using RelatedEntitiesQuery property of RetrieveRequest . You also specify the codition for related entity which can’t be done using LinkedEntity. It is a simple code where you need to specify the relationship name between two entities:

QueryExpression query = new QueryExpression();
query.EntityName = "systemuser";
query.ColumnSet = new ColumnSet("systemuserid");
Relationship relationship = new Relationship();

query.Criteria = new FilterExpression();
query.Criteria.AddCondition(new ConditionExpression("isdisabled", ConditionOperator.Equal, false));
// name of relationship between team & systemuser
relationship.SchemaName = "teammembership_association";
RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
relatedEntity.Add(relationship, query);
RetrieveRequest request = new RetrieveRequest();
request.RelatedEntitiesQuery = relatedEntity;
request.ColumnSet = new ColumnSet("teamid");
request.Target = new EntityReference
{
Id = teamId,
LogicalName = "team"

};
RetrieveResponse response = (RetrieveResponse)service.Execute(request);


if (((DataCollection<Relationship,EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities)))).Contains(new Relationship("teammembership_association")) && ((DataCollection<Relationship,EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities))))[new Relationship("teammembership_association")].Entities.Count > 0)
return true;
else
return false
;


RelatedEntityData

5 comments:

Ryno said...

Hi Im new to this but where did you get this

Id = teamId,

is it the GUID if the team entity?

B Johnson said...

Find the team's Guid that you want to find systemusers linked to.

Anwar said...

Thank you so much.

Asad said...

Nice Post.

below post accomplish it by using LINQ

http://microxrm.blogspot.in/2013/07/retrieve-nn-records-in-ms-crm-2011.html

Regards,

Yusuf

rani jadhav said...

Great Job...