Reputation: 1042
Is it possible to return a list of relationships on a particular type of entity (not an individual record) using the CRM/XRM SDK?
For example, if I have an entity called "Case" and I need to see if it has a relationship with "MyCustomEntity" is it possible to query the schema of the "Case" entity in CRM and find a list of relationships, checking for one relating to "MyCustomEntity". Similar to expanding the "1:N Relationships" tab in Customisations.
I Have found this article https://msdn.microsoft.com/en-us/library/gg509021.aspx which explains creating a new relationship, but nowhere to say "These are the relationships that X has"
Upvotes: 0
Views: 3970
Reputation: 1888
If you retrieve the entity via the metadata service, you can get to the relationships through the EntityMetadata property on the RetrieveEntityResponse. Here is an example:
RetrieveEntityRequest retrieveBankAccountEntityRequest = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.Relationships,
LogicalName = "account"
};
RetrieveEntityResponse retrieveBankAccountEntityResponse = (RetrieveEntityResponse)service.Execute(retrieveBankAccountEntityRequest);
var oneToNRelationships = retrieveBankAccountEntityResponse.EntityMetadata.OneToManyRelationships;
Here's a list of the properties of the response: https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.metadata.entitymetadata_members.aspx
Upvotes: 5