Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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
    Vipin J 1,583 on at
    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:
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    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>

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    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?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans