Skip to main content
Post a question

Notifications

Community site session details

Community site session details

Session Id : wGDAr9l20UYGTPPawYEnG5
Microsoft Dynamics CRM (Archived)

How to query data with many to many relationship with CRM Web API

Like (0) ShareShare
ReportReport
Posted on 7 Jun 2017 01:28:07 by

There is an entity with name st_salesimpersonate and another entity with name st_salesteam. The two entities are many-to-many relationship, and Relationship Entity Name is st_st_salesimpersonate_st_salesteam. How can I query the relationship data, it seems that the Relationship Entity Name is useless here.

*This post is locked for comments

  • Suggested answer
    Vipin J Profile Picture
    1,601 on 28 Nov 2018 at 17:20:48
    RE: How to query data with many to many relationship with CRM Web API
    I have created a generic method with detail explanation to get data about entity, which are in many to much relationship. Please refer following blog:
  • Goloknath Profile Picture
    on 07 Jun 2017 at 02:20:32
    RE: How to query data with many to many relationship with CRM Web API

    Your primary entity will be st_salesimpersonate

    and link entity will be st_salesteam

    something like below:

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">

     <entity name="st_salesimpersonate ">

       <attribute name="fullname" />

       <attribute name="contactid" />

       <order attribute="fullname" descending="false" />

       <link-entity name="st_salesteam" from="accountid" to="parentcustomerid" alias="ab">

         <attribute name="accountnumber" />

         <filter type="and">

           <condition attribute="accountnumber" operator="not-null" />

         </filter>

       </link-entity>

     </entity>

    </fetch>

  • Goloknath Profile Picture
    on 07 Jun 2017 at 02:03:08
    RE: How to query data with many to many relationship with CRM Web API

    Is there any way I can find/see the link entity?

  • Goloknath Profile Picture
    on 07 Jun 2017 at 02:02:18
    RE: How to query data with many to many relationship with CRM Web API

    but I don't know the attribute name of the link entity.

  • Goloknath Profile Picture
    on 07 Jun 2017 at 01:55:22
    RE: How to query data with many to many relationship with CRM Web API

    Hi Summer,

    If you are using early bound entities you can use syntax like below:

    (from ff in serviceContext.FirstEntitySet
    join r in serviceContext.SecondEntitySet on ff.FirstEntityId.Id equals r.SecondEntityId
    where r.Name == 'SomeName'
    select ff)

    For query using fetch XML try below (As suggested on https://msdn.microsoft.com/en-us/library/gg328446.aspx) :

     StringBuilder linkFetch = new StringBuilder();
                        linkFetch.Append("<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"true\">");
                        linkFetch.Append("<entity name=\"role\">");
                        linkFetch.Append("<attribute name=\"name\"/>");
                        linkFetch.Append("<link-entity name=\"systemuserroles\" from=\"roleid\" to=\"roleid\" visible=\"false\" intersect=\"true\">");
                        linkFetch.Append("<filter type=\"and\">");
                        linkFetch.Append("<condition attribute=\"systemuserid\" operator=\"eq\" value=\"" + _userId + "\"/>");
                        linkFetch.Append("</filter>");
                        linkFetch.Append("</link-entity>");
                        linkFetch.Append("</entity>");
                        linkFetch.Append("</fetch>");

    Thanks, 

    SM,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,729 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,718 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans