Recently I had to create a query that for a given primary entity would return a set of related target entities and support both 1-many and many-many relationships.
I knew how to do this in separate queries, so I could have some branching logic to see which type of query I needed to perform but that wasn’t a particularly elegant solution. I found the RelationshipQueryCollection, this allows you create a RetrieveRequest against your primary entity and then link in any related targets. All you have to do is specify the relationship name and it will support both 1-many and many-many relationships. If your are using self-referencing relationship be sure the populate relationship.PrimaryEntityRole.
Edit based on comment from daryllabar:
To better explain this function here is a practical example.
- There is a one to many relationship between contact and account, it is named “account_primary_contact”.
- The foreign key on the account is…
View original post 212 more words