I'd like to make an advanced search that combines fields from two different entities that are in many-to-many relation to each other. In SQL it's very straight-forward but in Dynamics I get stuck.
I'd like to get a general answer but if it helps, here are the specifics. The task is to create a list of all the contacts in the DB (column "name" and "e-mail") but combined with a third column that is the name of the marketing list that the contact in question belongs to. Of course, the same person might be listed multiple times but the uniqueness is not a requirement (besides, each row will have an equivalent to PK by the combination of the names of the contact and the marketing list).
Is that doable? If so, how?
Firstly, here's your query:
<fetch mapping='logical'>
<entity name='listmember'>
<link-entity name='list' from='listid' to='listid'>
<attribute name='listname'/>
</link-entity>
<link-entity name='contact' from='contactid' to='entityid'>
<attribute name='fullname'/>
<attribute name='emailaddress1'/>
</link-entity>
</entity>
</fetch>
Example results:
<resultset morerecords='0' paging-cookie='<cookie page="1"><listmemberid last="{E1B03485-0000-0000-0000-00155D107003}" first="{A74C877A-0000-0000-0000-00155D107003}" /></cookie>'>
<result>
<listmemberid>{A74C877A-0000-0000-0000-00155D107003}</listmemberid>
<listid.listname>List 1</listid.listname>
<entityid.fullname>Amy Pickle</entityid.fullname>
<entityid.emailaddress1>apickle@domain.com</entityid.emailaddress1>
</result>
<result>
<listmemberid>{A94C877A-0000-0000-0000-00155D107003}</listmemberid>
<listid.listname>List 2</listid.listname>
<entityid.fullname>Andrew Colley</entityid.fullname>
<entityid.emailaddress1>colley@domain.com</entityid.emailaddress1>
</result>
</resultset>
Not sure how well I can describe this in general terms but:
listmember
must be the root entity you returnlink-entity
(in this case, one to list
to get the list name and another to contact
to get contact's detailsattribute
s, so they each need to be defined within your link-entity
I never do these by hand. Previously I used James Downey's FetchXML Builder but I believe this is no longer available - Stunnware do a similar tool.